我正在尝试在 recyclerview 中使用 gridlayoutmanager 制作如下 UI:
+-------+ +-------+ | | | | | | | | | | | | +-------+ +-------+ +-----------------+ | | | | +-----------------+
我在这里调整 onBindViewHolder 的宽度:
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);
我尝试了多种方法: 1. 带有 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;
}
}
});
我最终是这样的:
+------------+ +-----------------+ +---- --------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------+ +-----------------+ +---- --------+
如果我不设置跨度,那么它最终会是这样的:
+------------+ +----------------+ | | | | | | | | | | | | | | | | | | | | +------------+ +----------------+ +---------------+ | | | | | | | | | | +---------------+
水平交错网格布局管理器。如果是使用
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams(); layoutParams.setFullSpan(true);
然后它变得与
+------------+ +-----------------+ +---- --------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------+ +-----------------+ +---- --------+
如果我最初不使用全跨度,它是完美的,但如果我滚动 recyclerview,它会变得与上面相同:
+------------+ +-----------------+ +---- --------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------+ +-----------------+ +---- --------+
我不明白是什么原因造成的。有人可以指出我可能犯了什么错误吗?