如何回流与StaggeredGridLayoutManager一个RecyclerView项 [英] How to reflow items in a RecyclerView with the StaggeredGridLayoutManager

查看:2137
本文介绍了如何回流与StaggeredGridLayoutManager一个RecyclerView项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的StaggeredGridLayoutManager玩耍,并有一些接近我想要的。我有2行,其中某些项目1行的高度,以及其他跨越两行的水平交错网格。我想单列高度物品叠起来,我认为这可以通过设置的差距战略GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS来实现,但是这似乎并没有工作。

电流:


 ___ ___ ___ ___
| ___ | | | | ___ | | |
       | | | |
       | ___ | | ___ |

我要什么:


 ___ ___ ___
| ___ | | | | |
 ___ | | | |
| ___ | | ___ | | ___ |

相关code片段:

设置的布局管理器的recyclerview:

  StaggeredGridLayoutManager的layoutManager =新StaggeredGridLayoutManager(2 StaggeredGridLayoutManager.HORIZONTAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(的layoutManager);

下面是onBindViewHolder为我的自定义适配器(这只是一个简单的例子)。基本上我有一个CardView(该CardView设置为wrap_content的高度和宽度)。

里面的ImageView的

 如果(位置%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(假);
}
其他{
    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,200,context.getResources()getDisplayMetrics());
    viewHolder.myImage.setLayoutParams(的LayoutParams);    StaggeredGridLayoutManager.LayoutParams layoutParams1 =((StaggeredGridLayoutManager.LayoutParams)viewHolder.itemView.getLayoutParams());
    layoutParams1.setFullSpan(真);
}


解决方案

也许我错了(有没有好的文档关于本在那里还),但我认为你不能做到这一点与此布局管理器。交错布局管理器可以让你把跨度内你的观点,如果你把一个占据了所有的跨度,它会计算基于当前的下一个视图位置(而不在previous考虑看看一)。

在换句话说,如果您将占据所有的跨距的视图,布局管理器会发现下一个视图最短的跨度,因为你占领了所有的人都用相同的观点,第一个跨度为候选

我希望这可以帮助你!

I'm playing around with the StaggeredGridLayoutManager and have something close to what I want. I have a horizontal staggered grid with 2 rows, where some items are the height of 1 row, and others span both rows. I want the single row height items to stack up and I thought that could be achieved by setting the gap strategy to GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS, but this does not appear to work.

Current:

 ___    ___    ___    ___
|___|  |   |  |___|  |   |
       |   |         |   |
       |___|         |___|

What I want:

 ___    ___    ___
|___|  |   |  |   |
 ___   |   |  |   |
|___|  |___|  |___|

The relevant code snippets:

Setting up the layout manager for the recyclerview:

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(layoutManager);

Here's the onBindViewHolder for my custom adapter (this is just a simplified example). Basically I have an ImageView inside of a CardView (the CardView is set to wrap_content for height and width).

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 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
    layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
    viewHolder.myImage.setLayoutParams(layoutParams);

    StaggeredGridLayoutManager.LayoutParams layoutParams1 = ((StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams());
    layoutParams1.setFullSpan(true);
}

解决方案

Maybe I'm wrong (there's no good documentation about this out there yet), but I think you cannot achieve that with this layout manager. The staggered layout manager lets you place your views within spans, and if you place one that occupies the all of the spans, it will calculate the position for the next view based on the current one (without taking a look at the previous one).

In other words, if you place a view that occupies all of the spans, the layout manager will find the shortest span for the next view, and because you occupied all of them with the same view, the first span is the candidate.

I hope this helps you!

这篇关于如何回流与StaggeredGridLayoutManager一个RecyclerView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆