I'm trying to use androids databinding feature in new project and so far very happy with it.
But now i came across a problem in my recyclerviews viewholder.
My viewholder uses different layouts (based on the viewtype when it gets created)
public MediaViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
switch(viewType){
case HEADER:
int layout = R.layout.item_media_header;
break;
case DEFAULT:
int layout = R.layout.item_media_default;
break;
case SMALL:
int layout = R.layout.item_media_small;
break;
}
View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
return new MediaViewHolder(v);
}
So all of those 3 layouts have the same views in it, only arranged differently. So the binding of the model to the views is the same.
Anyways, based on those layouts android creates
- ItemMediaHeaderBinding
- ItemMediaDefaultBinding
- ItemMediaSmallBinding
Which sucks since it would force me to create 3 different ViewHolder classes or instantiate the right binding Class by checking which layout is used.
Is there a best practice for this case? Is there a possibility to simply create a superclass for those three binding classes like "ItemMediaBinding".
Thanks in advance.