Here is what I have achieved ? 3 different sections, 10 different items in each section.
Here is the tutorial link I am following and below is the Screenshot:
Trying to show different Views
for each and every Section. Like:
For Section 1 (layout_1.xml)
For Section 2 (layout_2.xml)
For Section 3 (layout_3.xml)
But showing layout view of layout_1.xml
in every Section... (Section 1, 2, 3)
May I know where I am doing mistake in my code, what I have missed ?
public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {
private ArrayList<SingleItemModel> itemsList;
private Context mContext;
public SectionListDataAdapter(Context context, ArrayList<SingleItemModel> itemsList) {
this.itemsList = itemsList;
this.mContext = context;
}
@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);
SingleItemRowHolder rowTWO = new SingleItemRowHolder(viewTWO);
return rowTWO;
case 2:
View viewTHREE = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_3, null, false);
SingleItemRowHolder rowTHREE = new SingleItemRowHolder(viewTHREE);
return rowTHREE;
}
return null;
}
@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {
SingleItemModel singleItem = itemsList.get(i);
holder.tvTitle.setText(singleItem.getName());
}
@Override
public int getItemCount() {
return (null != itemsList ? itemsList.size() : 0);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle;
protected ImageView itemImage;
public SingleItemRowHolder(View view) {
super(view);
this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With