It's maybe a discussion not a question.
Normal way to implement multiple types
As you know, if we want to implement multiple types in RecyclerView
, we should provide multiple CustomViewHolder
extending RecyclerView.ViewHolder
.
For exmpale,
class TextViewHolder extends RecyclerView.ViewHolder{
TextView textView;
}
class ImageViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
}
Then we have to override getItemViewType
.And in onCreateViewHolder
to construct TextViewHolder
or ImageViewHolder
.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
return new ImageViewHolder(mLayoutInflater.inflate(R.layout.item_image, parent, false));
} else {
return new TextViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
}
}
Above code is normal but there is a another way.
Another way
I think only one CustomViewHolder
is enough.
class MultipleViewHolder extends RecyclerView.ViewHolder{
TextView textView;
ImageView imageView;
MultipleViewHolder(View itemView, int type){
if(type == 0){
textView = (TextView)itemView.findViewById(xx);
}else{
imageView = (ImageView)itemView.findViewById(xx);
}
}
}
Which way do you use in your developing work?