如何将回收器高度设置为 recyclerView 中的最高项目? [英] How to set recycler height to highest item in recyclerView?
问题描述
我需要确保水平 recyclerView 高度与最大项目的高度相同.
I need to make sure that horizontal recyclerView height is the same as the height of the biggest item.
Items 可以有不同的高度(Item = 总是相同的图像 + 标题 + 副标题,标题和副标题可以有无限长).当我为 recyclerView 设置 wrap_content 时,它会根据可见项目的高度调整大小,这会使 recyclerView 下方的内容发生跳跃,这是我想避免的.
Items can have a different height (Item = always the same image + title + subtitle, title and subtitle could have infinite length). When I set wrap_content for my recyclerView it would resize, basing on the height of visible items which makes content below recyclerView jump, and that's something I want to avoid.
我想要达到的目标:灰色区域是可见视口.
What I want to achieve: The gray area is visible viewport.
所以基本上我想以某种方式获得最大项目的高度,然后将 recyclerView 高度设置为该数字.
So basically I would like to get somehow hight of the biggest item, then put recyclerView height to that number.
我已经尝试过基于标题+副标题长度的近似值,但它非常不准确,因为例如,即使两个标题具有相同的文本长度,它们也可能具有不同的宽度,因为我使用的字体不是等宽字体.
What I already tried is approximation high of items based on length of title + subtitle but it's very inaccurate because for example even if two titles have the same text length they could have different width because of font that I use which is not a monospace font.
推荐答案
我也刚遇到这个问题.我的解决方案是:
I just had this issue as well. My solution is:
- 将 RecyclerView 包裹在 ConstraintLayout 中.
- 将 ConstraintLayout 的 layout_height 设置为 wrap_content.
- 向 ConstraintLayout 添加一个项目视图,并使用您希望根据其标题长度达到最高的项目的数据填充它.
- 将项目视图的可见性设置为不可见.
- 将 RecyclerView 的 layout_height 设置为零,并使其顶部和底部约束与项目视图的约束相匹配.
这篇关于如何将回收器高度设置为 recyclerView 中的最高项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!