具有 2x1 排列的 Gridlayoutmanager
Gridlayoutmanager with 2x1 arrangement
我正在尝试使用 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 的水平 gridlayoutmanager:
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,它就会变得与上面相同:
+------------+ +-----------------------------+ +------------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+------------+ +-----------------------------+ +------------+
我不明白是什么原因造成的。有人可以指出我可能犯了什么错误吗?
您想要实现水平回收器视图,其中重复出现这三个框。但是您将这 3 个框视为单独的回收器项目的方法不允许您将它们成组滚动。您需要做的是定义一个单独的布局,其中包含这 3 个框。然后为它提供 3 个项目的数据并将其绑定到视图持有者
基本上,您必须操作要分组的数据,然后显示它。
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == GROUP_ITEM) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.group_item, parent, false);
return new GroupViewHolder(view);
} else if (viewType == SINGLE_ITEM) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_item, parent, false);
return new SingleViewHolder(view);
}
return null;
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof GroupViewHolder) {
((GroupViewHolder) holder).populateData(position);
} else if (holder instanceof SingleViewHolder) {
// populate your normal view.
}
}
我正在尝试使用 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 的水平 gridlayoutmanager:
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,它就会变得与上面相同:
+------------+ +-----------------------------+ +------------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------+ +-----------------------------+ +------------+
我不明白是什么原因造成的。有人可以指出我可能犯了什么错误吗?
您想要实现水平回收器视图,其中重复出现这三个框。但是您将这 3 个框视为单独的回收器项目的方法不允许您将它们成组滚动。您需要做的是定义一个单独的布局,其中包含这 3 个框。然后为它提供 3 个项目的数据并将其绑定到视图持有者
基本上,您必须操作要分组的数据,然后显示它。
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == GROUP_ITEM) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.group_item, parent, false);
return new GroupViewHolder(view);
} else if (viewType == SINGLE_ITEM) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_item, parent, false);
return new SingleViewHolder(view);
}
return null;
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof GroupViewHolder) {
((GroupViewHolder) holder).populateData(position);
} else if (holder instanceof SingleViewHolder) {
// populate your normal view.
}
}