In your adapter class you should do these changes:
In your case you should override the getItemViewType
method
@Override
public int getItemViewType(int position) {
if (position < arrayList.size()) {
return ITEM_X_TYPE;
} else
return ITEM_Y_TYPE;
}
And in your onCreateViewHolder
:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_X_TYPE) {
return new XVH(mView);
} else if (viewType == ITEM_Y_TYPE) {
return new YVH(mView);
}
throw new RuntimeException("type not found");
}
And in onBindViewHolder
do this:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof XVH) {
XVH m = (XVH) holder;
... (continue your code)
EDIT:
Now what I suggest is that in your onCreateViewHolder
you should do smth like this:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_linear_layout_container, parent, false);
if (viewType == ITEM_X_TYPE) {
return new XVH(mView,ITEM_X_TYPE,Object one);
} else if (viewType == ITEM_Y_TYPE) {
return new XVH(mView,ITEM_Y_TYPE, Object two, Object three);
}
throw new RuntimeException(type not found");
}
So here you have created a view and you pass that to your view holder and create different instances of your view holder calling different constructors.
Then inside your VH constructor you do smth like this:
public class XVH extends RecyclerView.ViewHolder implements {
int viewType;
public XVH(View itemView,int viewType,Object one) {
super(itemView);
this.viewType = viewType;
// manage your layouts here, build there here or you can inflate from other xml
}
public XVH(View itemView,int viewType,Object two, Object three) {
super(itemView);
this.viewType = viewType;
// manage your layouts here, build there here or you can inflate from other xml
}
And in your onBindViewHolder
you check what item type is and then you load your data.
I hope this helps you