I have to create a heterogeneous RecyclerView. The default number of columns is 2, but some items require the full width. I have it while using StaggeredGridLayoutManager like this:
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
FeedItems currentItem = getItem(position);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
switch (viewType) {
case ITEM_TYPE_HEADER:
final HeaderItem headerItem = currentItem.getHeaderItem();
layoutParams.setFullSpan(true);
}
}
How do I do the same thing for GridLayoutManager, where I can easily switch the spans based of different view types?
There is a method void setSpanSizeLookup (GridLayoutManager.SpanSizeLookup spanSizeLookup). Use it like this:
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case 1:
return 1;
case 2:
return 2;
default:
return -1;
}
}
});
mRecyclerView.setLayoutManager(mLayoutManager);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With