RecyclerView Multiple Layout View(s) in an Adapter class
Use this inside adapter's getItemViewType
:
@Override public int getItemViewType(int position) { if (position == 0) { return 0; } else if(position == 1) { return 1; } else { return 2; } }
to use multiple layouts according to position in recyclerview you have to override the getItemViewType(int position) method inside the adapter :-
@Override public int getItemViewType(int position) { if(position==0) return 0; else if(position==1) return 1; else return 2; }
FYI
RecyclerView can also be used to inflate multiple view types .
- It will be easiest for you that create different Holder.
- Create Different Adapter is best Solutions
Try with
@Override public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) { switch (i) { case 0: View viewONE = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_1, null, false); SingleItemRowHolder rowONE = new SingleItemRowHolder(viewONE); return rowONE; case 1: View viewTWO = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_2, null, false); SingleItemRowHolderTwo rowTWO = new SingleItemRowHolderTwo (viewTWO); return rowTWO; case 2: View viewTHREE = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_3, null, false); SingleItemRowHolderThree rowTHREE = new SingleItemRowHolderThree(viewTHREE); return rowTHREE; } return null; }
Read RecyclerView can also be used to inflate multiple view types