问题描述
我试图做一个水平滚动的网格。它有两行。第二行偏移(由一个项目的宽度的一半,但是这是微不足道的计算并在这里并不重要)。
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从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.
这应该结束了看起来像与加上偏移量的项目在适配器位置0。你会得到所需要的输出ItemDecoration。 (通过 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重置历史计算。相反,使用详细的通知事件,如 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 - 横,两排网格,第二行偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!