RecyclerView - 横,两排网格,第二行偏移 [英] RecyclerView - horizontal, two-row grid, second row offset

查看:2706
本文介绍了RecyclerView - 横,两排网格,第二行偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个水平滚动的网格。它有两行。第二行偏移(由一个项目的宽度的一半,但是这是微不足道的计算并在这里并不重要)。

I am trying to make a horizontally scrolling grid. It has two rows. The second row is offset (by half the width of one item, but that is trivial to calculate and doesn't matter here).

我目前使用RecyclerView和GridLayoutManager从<一个href=\"https://github.com/antoniolg/RecyclerViewExtensions/tree/master/library/src/main/java/com/antonioleiva/recyclerviewextensions\" rel=\"nofollow\">https://github.com/antoniolg/RecyclerViewExtensions/tree/master/library/src/main/java/com/antonioleiva/recyclerviewextensions

I am currently using RecyclerView and the GridLayoutManager from https://github.com/antoniolg/RecyclerViewExtensions/tree/master/library/src/main/java/com/antonioleiva/recyclerviewextensions

不过,抵消被证明是非常困难的。

However, the offsetting is proving extremely difficult.

这应该结束了看起来像 http://i.imgur.com/XrbnV5x.png

It should end up looking like http://i.imgur.com/XrbnV5x.png

是否有人有用于使第二排交错如上面的图片有什么建议?

Does anybody have any suggestions for making the second row staggered like in the picture above?

推荐答案

使用<一个href=\"https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html\"相对=nofollow> StaggeredGridLayoutManager 与加上偏移量的项目在适配器位置0。你会得到所需要的输出ItemDecoration。 (通过<一个href=\"https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration.html#getItemOffsets(android.graphics.Rect,%20android.view.View,%20android.support.v7.widget.RecyclerView,%20android.support.v7.widget.RecyclerView.State)\"相对=nofollow> getItemOffsets 的)。
可替代地,代替一个ItemDecoration,当onBind被调用时,可以设置第一项的宽度,使得其将包括左侧间隙

Use StaggeredGridLayoutManager with an ItemDecoration which adds the offset to the item at adapter position 0. You'll get the desired output. (via getItemOffsets). Alternatively, instead of an ItemDecoration, when onBind is called, you can set the first item's width such that it will include gap on left.

如果您的数据集的变化,不叫notifyDataSetChanged将在SGLM重置历史计算。相反,使用详细的通知事件,如<一个href=\"https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyItemRangeInserted(int,%20int)\"相对=nofollow> notifyItemRangeInserted 使SGLM就能重新计算布局而不重设职务。

If your data set changes, don't call notifyDataSetChanged which will reset the historical calculations in SGLM. Instead, use detailed notify events like notifyItemRangeInserted so that SGLM will be able to recalculate layout without resetting positions.

这篇关于RecyclerView - 横,两排网格,第二行偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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