我正在玩StaggeredGridLayoutManager并且有一些接近我想要的东西.我有一个水平交错网格有2行,其中一些项目是1行的高度,其他项目跨越两行.我想要将单行高度项目叠加起来,我认为可以通过将差距策略设置为GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS来实现,但这似乎不起作用.
当前:
___ ___ ___ ___
|___| | | |___| | |
| | | |
|___| |___|
我想要的是:
___ ___ ___ |___| | | | | ___ | | | | |___| |___| |___|
相关代码片段:
为recyclerview设置布局管理器:
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(layoutManager);
Run Code Online (Sandbox Code Playgroud)
这是我的自定义适配器的onBindViewHolder(这只是一个简化的例子).基本上我在CardView中有一个ImageView(CardView设置为wrap_content的高度和宽度).
if(position%3==0) {
ViewGroup.LayoutParams layoutParams = viewHolder.myImage.getLayoutParams();
layoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 75, context.getResources().getDisplayMetrics());
viewHolder.myImage.setLayoutParams(layoutParams);
StaggeredGridLayoutManager.LayoutParams layoutParams1 = ((StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams());
layoutParams1.setFullSpan(false);
}
else {
ViewGroup.LayoutParams layoutParams = viewHolder.myImage.getLayoutParams();
layoutParams.width = …Run Code Online (Sandbox Code Playgroud) 在我的2列交错网格中,我使用无限列表的效果自动按10乘10加载元素,除了向上滚动时,其他所有东西都工作正常。前两个元素更改其位置。就像前两个项目从一列跳到另一列一样。
如我所读,适配器必须保存每个元素的状态。但是,我不知道该怎么做。
这是我用来显示StaggeredGrid功能的主要代码
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space+1;
// Add top margin only for the first item to avoid double space between items
if(parent.getChildLayoutPosition(view) == 0)
outRect.top = space;
}
}
public class SolventViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView vh_name;
public TextView vh_price;
public ImageView …Run Code Online (Sandbox Code Playgroud) android vertical-scrolling staggered-gridview android-recyclerview