How to make card style menu in android - java

I want to add card style in my app like this
i use in my app mysql database so i need to make like this cards and put my data from database in it now i use ListView with this code
public void listAllItme() {
ListAdapter lA = new listAdapter(listitems);
listView.setAdapter(lA);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent open = new Intent(R_arabic.this, rewaya_show.class);
open.putExtra("name", listitems.get(position).name);
open.putExtra("url", listitems.get(position).url);
open.putExtra("img", listitems.get(position).img);
open.putExtra("num", listitems.get(position).num);
startActivity(open);
}
}
});
}
class listAdapter extends BaseAdapter {
ArrayList<listitem_gib> lista = new ArrayList<listitem_gib>();
public listAdapter(ArrayList<listitem_gib> lista) {
this.lista = lista;
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.row_item_gib, null);
TextView name = (TextView) view.findViewById(R.id.textView_gib);
ImageView img = (ImageView) view.findViewById(R.id.imageView_gib);
TextView num = (TextView) view.findViewById(R.id.textView_gib2);
TextView size = (TextView) view.findViewById(R.id.textView_gib3);
name.setText(lista.get(position).name);
num.setText(lista.get(position).num);
size.setText(lista.get(position).size);
Picasso.with(R_arabic.this).load("http://grassyhat.com/android/image/" + lista.get(position).img).into(img);
return view;
}
}
first i want to know how i can make like this card style
second how i can use this code with card menu not listview
sorry im new in android and sorry for my bad english

What do you mean by card menu? because the example in the image is a recyclerview with a cardview item, you can achieve this by doing something like this
This will be your activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//Just your list of objects, in your case the list that comes from the db
List<Items> itemsList = new ArrayList<>();
CardAdapter adapter = new CardAdapter(this, itemsList);
//RecyclerView needs a layout manager in order to display data so here we create one
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//Here we set the layout manager and the adapter to the listview
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Inside the layout file you just have to place the recyclerview like this
<RelativeLayout
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="jsondh.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Then your adapter will be something like this
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private List<Items> itemsList;
private Activity activity;
public CardAdapter(Activity activity, List<Items> items){
this.activity = activity;
this.itemsList = items;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = activity.getLayoutInflater().inflate(R.layout.cardview_layout, parent, false);
return new CardViewHolder(itemView);
}
#Override
public void onBindViewHolder(CardViewHolder holder, int position) {
//Here you bind your views with the data from each object from the list
}
#Override
public int getItemCount() {
return itemsList.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
public ImageView bookImage;
public TextView bookLabel01, bookLabel02;
public CardViewHolder(View itemView) {
super(itemView);
bookImage = (ImageView)itemView.findViewById(R.id.image);
bookLabel01 = (TextView)itemView.findViewById(R.id.label01);
bookLabel02 = (TextView)itemView.findViewById(R.id.label02);
}
}
And the last one will be the layout from each item on the list, like this
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="15dp"
app:cardBackgroundColor="#3369Ed">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="150dp"
android:layout_height="130dp"/>
<TextView
android:id="#+id/label01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
<TextView
android:id="#+id/label02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LongerLabel"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</android.support.v7.widget.CardView>
You also have to add this to your gradle file:
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
Hope it helps!

It's pretty simple. You will have to use a RecyclerView with GridLayoutManager and add a cardView to it.
Then, use an Adapter and ViewHolder to feed the data.
I suggest you to check this out:
https://developer.android.com/training/material/lists-cards.html

Related

How to add a spinner to each item inside a recyclerview?

I am trying to add a spinner object inside each item of a recyclerview.
I think the problem is that in OnCreate the setContentView(R.layout.activity_modify_list2) is referring to the xml file that contains the recyclerview:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify_list2);
initView();
}
Because of this, the createSpinner() method is looking for the spinner in that file rather than the xml file for the individual recyclerview item, giving a null object reference error:
private Spinner CreateSpinner() {
Spinner staticSpinner = findViewById(R.id.spinner);
... }
Full java file for the page:
public class ModifyListActivity extends Activity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify_list2);
initView();
}
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerViewModifyList);
createList();
}
private void createList() {
ArrayList<ModifyListPageItem> items = new ArrayList<>();
ModifyListPageItem item;
item = new ModifyListPageItem();
item.setTitle("Resident Evil Games");
item.setDescription("Ranking all Resident Evil games");
items.add(item);
item = new ModifyListPageItem();
item.setRank("1.");
item.setSpinner(CreateSpinner());
items.add(item);
item = new ModifyListPageItem();
item.setRank("2.");
item.setSpinner(CreateSpinner());
items.add(item);
item = new ModifyListPageItem();
item.setRank("3.");
item.setSpinner(CreateSpinner());
items.add(item);
// set adapter
ModifyListPageAdapter adapter = new ModifyListPageAdapter(this, items);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
private Spinner CreateSpinner() {
Spinner staticSpinner = findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.resident_evil_games,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
return staticSpinner;
}
}
Full Java file for adapter
public class ModifyListPageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static int TYPE_HEADER = 1;
private static int TYPE_ITEM = 2;
private Context context;
private ArrayList<ModifyListPageItem> items;
public ModifyListPageAdapter(Context context, ArrayList<ModifyListPageItem> items) {
this.context = context;
this.items = items;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == TYPE_HEADER) {
view = LayoutInflater.from(context).inflate(R.layout.item_modlist_header, parent, false);
return new HeaderViewHolder(view);
}
else {
view = LayoutInflater.from(context).inflate(R.layout.item_modlist_object, parent, false);
return new ItemViewHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == TYPE_HEADER)
((HeaderViewHolder)holder).SetHeaderDetails(items.get(position));
else
((ItemViewHolder)holder).SetItemDetails(items.get(position));
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
if (TextUtils.isEmpty(items.get(position).getRank())) {
return TYPE_HEADER;
}
else
return TYPE_ITEM;
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
private TextView txtTitle;
private TextView txtDescription;
public HeaderViewHolder(#NonNull View itemView) {
super(itemView);
txtTitle = itemView.findViewById(R.id.txtModListTitle);
txtDescription = itemView.findViewById(R.id.txtModListDescription);
}
private void SetHeaderDetails(ModifyListPageItem item) {
txtTitle.setText(item.getTitle());
txtDescription.setText(item.getDescription());
}
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView txtRank;
private Spinner spinner;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
txtRank = itemView.findViewById(R.id.modlist_rank);
spinner = itemView.findViewById(R.id.spinner);
}
private void SetItemDetails(ModifyListPageItem item) {
txtRank.setText(item.getRank());
spinner.setOnItemClickListener(spinner.getOnItemClickListener());
}
}
}
XML file for the page layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ModifyListActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_navigation"
app:itemBackground="#color/colorPrimary"
app:itemTextColor="#drawable/selector"
app:itemIconTint="#drawable/selector"
app:menu="#menu/menu_navigation"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerViewModifyList"/>
</RelativeLayout>
XML File for the recyclerview item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/modlist_rank"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="1."
android:textColor="#000000"
android:textSize="38dp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp" />
</LinearLayout>

Custom Adapter is not showing anything from a Fragment [duplicate]

This question already has answers here:
ListView not showing at all
(1 answer)
Null pointer Exception - findViewById()
(12 answers)
Closed 4 years ago.
I have a custom Adapter called MyAdapter where i'm trying to load a layout which supposedly should be inserted into a listview, but even when Android is not giving errors the view is showing in blank.
In the next screenshot, i show what i get after build my app.
What i'm expecting to load is my layout into my listview:
ListView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.NewsFragment">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/list_item"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageabdcf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="25dp"
tools:srcCompat="#android:drawable/btn_star_big_on" />
<TextView
android:id="#+id/textabcdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
NewsFragment.java
public class NewsFragment extends Fragment {
private ListView listView;
private List<String> names;
public NewsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_news, container, false);
listView = (ListView) v.findViewById(R.id.listView);
names = new ArrayList<String>();
names.add("Fernando");
names.add("Roberto");
names.add("Torres");
names.add("Urban");
ArrayAdapter<String> adapter = new ArrayAdapter<>(v.getContext(), android.R.layout.simple_list_item_1, names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getView().getContext(), "Clicked: " + names.get(position), Toast.LENGTH_LONG).show();
}
});
MyAdapter myAdapter = new MyAdapter(v.getContext(), R.layout.card_view_news, names);
listView.setAdapter(myAdapter);
// Inflate the layout for this fragment
return v;
}
}
MyAdapter.java
public class MyAdapter extends BaseAdapter {
private Context context;
private int layout;
private List<String> names;
public MyAdapter(Context context, int layout, List<String> names) {
this.context = context;
this.layout = layout;
this.names = names;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return this.names.get(position);
}
#Override
public long getItemId(int id) {
return id;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View v = convertView;
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
v = layoutInflater.inflate(R.layout.fragment_news, null);
String currentName = names.get(position);
//currentName = (String) getItem(position);
TextView textView = (TextView) v.findViewById(R.id.textabcdf);
textView.setText(currentName);
return v;
}
}
And what i'm expecting for is something like:

Trying to create a custom ArrayAdapter with ArrayList

I'm trying to get ArrayList with different variables that i'm getting from a database to display in a customized listview
this is the main xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View the balance sheet"
android:textSize="29sp"
android:id="#+id/view"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView">
</ListView>
</RelativeLayout>
this is the customized layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/description_names"
android:text="description"
android:layout_marginTop="25sp"
android:layout_marginLeft="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/values_names"
android:text="value"
android:layout_marginTop="25sp"
android:layout_marginLeft="105sp"/>
</RelativeLayout>
this is the adapter, i wanted both the ArrayLists from the main activity but it's not working
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(#NonNull Context context, ArrayList<String> mDetails,ArrayList<String> mValues) {
super(context,R.layout.custom_layout ,mDetails);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(getContext());
View customView=inflater.inflate(R.layout.custom_layout,parent,false);
String details=getItem(position);
String values=getItem(position);
TextView description_names=(TextView)customView.findViewById(R.id.description_names);
TextView values_names=(TextView)customView.findViewById(R.id.values_names);
description_names.setText(details);
return customView;
}
}
main activity
ListView listvIew=(ListView)findViewById(R.id.listView);
final ArrayAdapter<String>array=new CustomAdapter(this,mDetails,mValues);
listvIew.setAdapter(array);
private ArrayList<String>mDetails=new ArrayList<>();
private ArrayList<String>mValues=new ArrayList<>();
V3Ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value=dataSnapshot.child("records").child("values").getValue(String.class);
Log.d(TAG, "onChildAdded: the value is"+value);
mDetails.add(value);
mValues.add(value);
array.notifyDataSetChanged();
}
i'm getting values from the database then putting them in ArrayLists then i'm trying to display them in a customized listview but it's not working when i try to use the second Arraylist
adapter calls from your main activity should be like,
CusatomeAdapter adapter;
adapter = new CusatomeAdapter(mActivity,mList1 ,mList2);
mListview.setAdapter(adapter);
here is your adapter calss
public class CusatomeAdapter extends BaseAdapter {
public ArrayList<String> mList1;
public ArrayList<String> mList2;
Activity activity;
public CusatomeAdapter(Activity activity,ArrayList<String> mList1 , ArrayList<String> mList2){
super();
this.activity = activity;
this.mList1 = mList1;
this.mList2 = mList2;
}
#Override
public int getCount() {
return mList1.size(); //give the size of list here
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_detail, null);
holder = new ViewHolder();
holder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
//same way define another text view and set their value ..
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_detail.setText(mList1.get(position).toString());
return convertView;
}
private class ViewHolder {
TextView tv_detail;
}
}

Recycle View adapter not loading the items

I am new to android I have created recycler view with its adapter but it is not working. check the following code.
I tried to debug this activity but there is no error. and also in OtherOfferingAdapter program don't execute further after getItemCount().
File OtherOfferingAdapter.java
public class OtherOfferingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<OtherOfferingData> data = Collections.emptyList();
OtherOfferingData current;
int currentPos=0;
//Create constructor to innitilize context and data sent from MainActivity
public OtherOfferingAdapter(Context context,List<OtherOfferingData> data){
this.context=context;
inflater = LayoutInflater.from(context);
this.data=data;
}
//Inflate the layout when viewholder created
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = inflater.inflate(R.layout.container_offerings, parent, false);
MyHolder holder = new MyHolder(view);
return holder;
}
//Bind Data
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position){
MyHolder myHolder = (MyHolder) holder;
current = data.get(position);
myHolder.inputOfferingName.setText(current.offeringname);
myHolder.inputOfferingPrice.setText(current.price);
myHolder.inputOfferingRating.setRating(Float.parseFloat(current.rating));
Picasso.with(context).load(current.imageUrl).into(myHolder.inputOfferingImage);
if(current.dietType.equals("1")){
myHolder.inputDietType.setBackgroundResource(R.drawable.veg);
}else{
myHolder.inputDietType.setBackgroundResource(R.drawable.nonveg);
}
}
#Override
public int getItemCount(){
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder{
ImageView inputOfferingImage, inputDietType;
TextView inputOfferingPrice, inputOfferingName;
RatingBar inputOfferingRating;
public MyHolder(View itemView){
super(itemView);
inputOfferingImage = (ImageView) itemView.findViewById(R.id.otherimage);
inputDietType = (ImageView) itemView.findViewById(R.id.otherdietType);
inputOfferingPrice = (TextView) itemView.findViewById(R.id.otherprice);
inputOfferingName = (TextView) itemView.findViewById(R.id.otherofferingnanme);
inputOfferingRating = (RatingBar) itemView.findViewById(R.id.otherrating);
}
}
}
File OtherOfferingData
public class OtherOfferingData {
public String imageUrl;
public String offeringname;
public String price;
public String dietType;
public String rating;
}
And I am calling adapter like this
recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
offeringAdapter = new OtherOfferingAdapter(ProductDescriptionActivity.this, data);
recyclerView.setAdapter(offeringAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(ProductDescriptionActivity.this));
container_offerings.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginBottom="30dp"
android:background="#color/lightGray"
android:paddingLeft="15dp"
android:paddingRight="15dp">
Activity Page
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycle_view"
android:layout_width="0dp"
android:layout_height="0dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/lightGray"
android:layout_marginBottom="90dp"/>
ThankYou
The size of your recycler view in the relative layout is 0dp try to change
android:layout_width="0dp"
android:layout_height="0dp"
both to match parent
android:layout_width="match_parent"
android:layout_height="match_parent"
Your recycler view and its adapter seems to be fine. But they don't appear on screen as they have 0dp as dimension.

Layout not refreshing from RecyclerView in LinearLayout

I've a RecyclerView in a LinearLayout which is in a ScrollView.
I get the items for the RecyclerView from the net so I've to notifyDataSetChanged.
This is working - I get the new data in the Adapter. But the view doesn't change.
Here the xml like I wrote above:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager_featured"
android:layout_width="match_parent"
android:layout_height="#dimen/featured_object_height"
android:layout_gravity="center" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
when I get the data...:
public void initAdapter(ArrayList<Stream> list) {
// initializing: passed the streams element
this.streams.addAll(list);
// the RecyclerView Adapter
mBitmovinAdapter.notifyDataSetChanged();
mLinearLayout.invalidate();
mRecyclerView.invalidate();
}
and my adapter..:
BitmovinAdapter(ArrayList<Stream> streams) {
this.mStreams = streams;
}
ArrayList<Stream> mStreams;
class BitmovinHolder extends RecyclerView.ViewHolder {
TextView title;
TextView format;
ImageView poster;
public BitmovinHolder(final View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
format = (TextView) view.findViewById(R.id.format);
poster = (ImageView) view.findViewById(R.id.poster);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BasePlayerActivity.launchPlayerActivity(view.getTag().toString(), title.getText().toString(), 0, MainActivity.debug, view.getContext());
}
});
}
}
#Override
public void onBindViewHolder(BitmovinHolder holder, int position) {
Stream stream = mStreams.get(position);
holder.title.setText(stream.title);
holder.format.setText(stream.format);
Glide.with(holder.poster.getContext()).load(stream.poster).into(holder.poster);
holder.itemView.setTag(stream.stream);
}
#Override
public BitmovinHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new BitmovinHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false));
}
#Override
public int getItemCount() {
return mStreams.size();
}
Set the data to adapter and then call notifyDataSetChanged.
mPagerAdapter.setData(streams);
mPagerAdapter.notifyDataSetChanged();
Try this In RecyclerView
Add this Method in your RecyclerView.Adapter
ArrayList<Stream> list
/*
* Inserting a new item at the head of the list. This uses a specialized
* RecyclerView method, notifyItemInserted(), to trigger any enabled item
* animations in addition to updating the view.
*/
public void addItem(int position) {
if (position > list.size()) return;
list.add(position, generateDummyItem());
notifyItemInserted(position);
}
Try something like adapter.clear() before adding new elements. Then call adapter.addAll().

Categories