I am trying to make a UI like the following using a gridlayoutmanager in a recyclerview:
+-------+ +-------+ | | | | | | | | | | | | +-------+ +-------+ +-----------------+ | | | | +-----------------+
I am adjusting width in onBindViewHolder here:
int mode = (position+1)%4; int h_mul = 1, w_mul = 1; switch (mode){ case 0: h_mul = w_mul = 2; break; case 2: w_mul = 2; break; case 3: case 1: default: h_mul = w_mul = 1; break; } mCardView.getLayoutParams().height = h_mul * mContext.getResources().getInteger(R.integer.video_tile_height); mCardView.getLayoutParams().width = w_mul * mContext.getResources().getInteger(R.integer.video_tile_width); I have tried a number of approaches: 1. Horizontal gridlayoutmanager with setSpanSizeLookup:
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { int mode = (position+1)%4; switch (mode){ case 0: case 2: return manager.getSpanCount(); case 3: case 1: return 1; default: return -1; } } }); What i end up is this:
+------------+ +-----------------------------+ +------------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------+ +-----------------------------+ +------------+
If i dont set span then it ends up like this:
+------------+ +---------------+ | | | | | | | | | | | | | | | | | | | | +------------+ +---------------+ +---------------------------+ | | | | | | | | | | +---------------------------+
Horizontal staggeredgridlayoutmanager. If is use
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams(); layoutParams.setFullSpan(true);
Then it becomes same as
+------------+ +-----------------------------+ +------------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------+ +-----------------------------+ +------------+
If i dont use full span initially it is perfect but if I scroll the recyclerview it becomes same as above:
+------------+ +-----------------------------+ +------------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------+ +-----------------------------+ +------------+
I don't understand what is causing this. Can someone please point out what mistake I might be making?
