包装布局上的GridLayoutManager不会调整RecyclerView的大小以显示项目(GridLayoutManager on a wrapped layout does not size

编程入门 行业动态 更新时间:2024-10-27 00:33:40
包装布局上的GridLayoutManager不会调整RecyclerView的大小以显示项目(GridLayoutManager on a wrapped layout does not size up the RecyclerView to display the items)

带有wrap_content和GridLayoutManager RecyclerView不显示项目。 它没有扩大为物品腾出空间。

首先,我注意到有一个问题(74772)开放 ,但截至2015年12月尚未解决, 直到“2016年初” 。

有人似乎已经制作了这个CustomGridLayoutManager ,也可以在Github上使用 ,但它似乎仍然没有为所有项目留出足够的空间,使得RecyclerView看起来被裁剪(但可滚动),即使在其父级中有足够的空间用于RecyclerView 。

有关如何使RecyclerView正确调整项目大小并显示它而不滚动的任何想法,如果可能的话?

RecyclerView with wrap_content and GridLayoutManager does not display the items. It doesn't enlarge to make room for the items.

First of all, I noticed there is an issue (74772) open for it, but it isn't solved yet as of December 2015, and not until “early 2016”.

Someone appeared to have made this CustomGridLayoutManager, also available on Github, but it still doesn't seem to make enough room for all items, making the RecyclerView appear cropped (but scrollable) even when there is enough room for the RecyclerView in its parent.

Any ideas as to how to make the RecyclerView properly size up the items and show it without scrolling, when that is possible?

最满意答案

在测量时,该类似乎只考虑第一个孩子和每行的第一个孩子(分配的任何尺寸取决于方向)。 看到这个(我的评论):

if (getOrientation() == HORIZONTAL) { if (i % getSpanCount() == 0) { // only for first child of row. width = width + mMeasuredDimension[0]; } if (i == 0) { // only for first item. height = mMeasuredDimension[1]; } }

取向相同的东西发生在方向VERTICAL (在随后的else捕获)。

为了满足我的需要,我测量了每个孩子,检查了每行中最大的孩子,然后对行进行了最大限制。 测量完每行后,将行大小添加到所需的总大小。

if (getOrientation() == VERTICAL) { rowMeasure[1] = Math.max(rowMeasure[1], childMeasure[1]); rowMeasure[0] += childMeasure[0]; } else { rowMeasure[0] = Math.max(rowMeasure[0], childMeasure[0]); rowMeasure[1] += childMeasure[1]; } // When finishing the row (last item of row), adds the row dimensions to the view. if (i % getSpanCount() == getSpanCount() - 1 || i == state.getItemCount() - 1) { rowsSized[addIndex] += rowMeasure[addIndex]; rowsSized[maxIndex] = Math.max(rowsSized[maxIndex], rowMeasure[maxIndex]); rowMeasure[addIndex] = 0; rowMeasure[maxIndex] = 0; }

完整的课程在本答案结尾处提供。 以上只显示了逻辑。

我还没有完全测试这个解决方案,因为我本周碰到了这个问题,并且试图解决它 - 至少在我的需求 - 昨天(08年12月)。

您可以在此处查看我如何使用WrappedGridLayoutManager解决此问题。

正如问题评论中所述,您必须使用其getItemCount()来关注RecyclerView的State 。 我还建议看一下getViewForPosition(int) ,看看是否/如何受到prelayouts条件的影响等等。

That class seems to take into account, when measuring, only the first child and the first child of each row (whichever dimension is assigned depends on the orientation). See this (comments by me):

if (getOrientation() == HORIZONTAL) { if (i % getSpanCount() == 0) { // only for first child of row. width = width + mMeasuredDimension[0]; } if (i == 0) { // only for first item. height = mMeasuredDimension[1]; } }

Something identical happens to orientation VERTICAL (catched in the else that follows).

In order to account for my needs, I measured each child, checking the largest child in each row, then applying the maximum constraints to the row. When each row was finished measured, then the row size was added to the overall size that was needed.

if (getOrientation() == VERTICAL) { rowMeasure[1] = Math.max(rowMeasure[1], childMeasure[1]); rowMeasure[0] += childMeasure[0]; } else { rowMeasure[0] = Math.max(rowMeasure[0], childMeasure[0]); rowMeasure[1] += childMeasure[1]; } // When finishing the row (last item of row), adds the row dimensions to the view. if (i % getSpanCount() == getSpanCount() - 1 || i == state.getItemCount() - 1) { rowsSized[addIndex] += rowMeasure[addIndex]; rowsSized[maxIndex] = Math.max(rowsSized[maxIndex], rowMeasure[maxIndex]); rowMeasure[addIndex] = 0; rowMeasure[maxIndex] = 0; }

The full class is available at the end of this answer. The above shows only the logic.

I did not fully tested this solution yet, as I bumped into this issue this week, and tried to solve it—again, at least for my needs—yesterday (dec 08).

You can check how I tackled this problem with my WrappedGridLayoutManager here.

As stated in the issue comments, you must pay attention to the RecyclerView's State, using its getItemCount() instead. I also recommend taking a look at getViewForPosition(int) to see if/how this may be affected by prelayouts conditions and so on.

更多推荐

本文发布于:2023-08-05 22:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441096.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布局   大小   项目   RecyclerView   GridLayoutManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!