I already checked this question Android DataBinding in RecyclerView.ViewHolders with different layouts
But in my case, the ViewHolders have a super class BaseViewHolder. All of the ViewHolders have the same outer layout, like a header and a close button but the content layout differ from viewtype to viewtype.
I want to use DataBinding and MVP to handle this views. I am able to get a Binder for every ViewHolder but every binding has a different class-type. All of them are extending ViewDataBinding.
Some code:
public ViewHolderA(ListItemABinding binding) extends BaseViewHolder{
super(binding);
... do stuff with the binding
}
public ViewHolderB(ListItemBBinding binding) extends BaseViewHolder {
super(binding);
... do stuff with the binding
}
public BaseViewHolder(ViewDataBinding binding) extends RecyclerView.ViewHolder {
super(binding.getRoot());
//access the header and the button here!!!
}
inside of the Adapter:
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int itemType) {
switch (itemType) {
case SmartHomeBase.VIEW_TYPE_A:
ListItemABinding bindingA = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.list_item_a, viewGroup, false);
return new ViewHolderA(bindingA);
default:
ListItemBBinding bindingB = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.list_item_b, viewGroup, false);
return new ViewHolderB(bindingB);
}
}
What I get from the DataBindingUtil is ListItemABinding and ListItemBBinding, both containing the Button and the Header I need in the BaseViewHolder. The BaseViewHolder just accept the ViewDataBinding where I can not reach (the easy way) the views I need.
Option 1: I can use findViewById (again) - but that seems not really nice.
Option 2: check instanceof for the DataBindings in BaseViewHolder and cast - seems also not really nice.
Is there an Option 3 I don't see?