How to handle many different types of views in recyclerview user view

What if I have 50 views? Should I have 50 static inner classes in my adapter? According to this answer , yes.

My first thought was to move each viewer's inner class to a separate public class, but they should be static. So, encapsulate each of them in an open class to make the inner class static? Are there any nicer alternatives?

edit: sample code: So would that be a good solution? Doesn't that kill performance?

public class MainViewHolder extends DragSortAdapter.ViewHolder implements View.OnClickListener, View.OnLongClickListener { View container; TextView title; //called in onCreateViewHolder in adapter public MainViewHolder(DragSortAdapter adapter, View itemView) { super(adapter, itemView); container = itemView.findViewById(R.id.card_root); title = container.findViewById(R.id.text); } //called by onBindViewHolder in adapter public void setData(Data data) { title.setText(data.title); } } 

edit2: sample when a new instance of the view object is returned

 @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case 0: return new MainViewHolder(...); case 2: return new MainViewHolderOther(...); ... } } 
+7
android adapter android-recyclerview
source share
1 answer

You can use only one inner viewHolder class to handle many different types of viewType. First, you must use getItemViewType (viewType) to get the position of the position and also use viewHolder according to the position of viewType.

Make one vieHolder inner class in which you can pass the view and view type according to the position of the element and inflate the layout as shown below. An example code is given below.

 public class Adapter extends RecyclerView.Adapter<Adapter.MyViewholder> { LayoutInflater inflater; private Context context; ArrayList<Detail> data = new ArrayList<Detail>(); public Adapter(Context context, ArrayList<Detail> getdata) { this.context = context; inflater = LayoutInflater.from(context); this.data = getdata; } class MyViewholder extends RecyclerView.ViewHolder { TextView txtRow, txt_rec; public MyViewholder(View view, int type) { super(view); if (type == Constants.NORMAL) { txtRow = (TextView) view .findViewById(R.id.txtRow); } else if (type == Constants.RECORDING) { txt_rec = (TextView) view .findViewById(R.id.txt_rec); } } } @Override public int getItemCount() { return data.size(); } @Override public void onBindViewHolder(MyViewholder viewholder, int position) { int listViewItemType = getItemViewType(position); Detail detail = data.get(listViewItemType); if (detail.getType() == Constants.NORMAL) { viewholder.txtRow.setText(detail.getKey()); } else if (detail.getType() == Constants.RECORDING) { viewholder.txt_rec.setText(detail.getRecording()); } } @Override public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) { int listViewItemType = getItemViewType(viewType); switch (listViewItemType) { case 0: view = inflater.inflate(R.layout.detail_item, parent, false); myViewholder = new MyViewholder(view, Constants.NORMAL); case 2: view = inflater.inflate(R.layout.recording, parent, false); myViewholder = new MyViewholder(view, Constants.RECORDING); } return myViewholder; } @Override public int getItemViewType(int position) { return position; } } 

Hope this code helps you

+8
source share

All Articles