GridView行重叠:如何使行高适合最高的项目?
就像前一个人一样,我的 GridView 项目之间存在不需要的重叠:
注意除了最右边的一列之外的每一列中的文本。
我与上一个问题的不同之处在于我不想要恒定的行高。我希望行高有所不同,以容纳每行中最高的内容,从而有效利用屏幕空间。
查看 GridView 源 (不是权威副本,但 kernel.org 仍然处于关闭状态),我们可以在 fillDown() 和 makeRow() 中看到最后看到的视图是“参考视图”:行的高度设置为该视图的高度,而不是最高的视图的高度。 这解释了为什么最右边的列没问题。不幸的是,GridView 没有很好地设置让我通过继承来解决这个问题。所有相关字段和方法都是私有的。
因此,在我采取“克隆并拥有”这一陈旧的臃肿路径之前,我在这里缺少一个技巧吗?我可以使用 TableLayout,但这需要我实现 numColumns="auto_fit"
我自己(因为我只想在手机屏幕上显示一长列),而且它也不会是 AdapterView,这感觉应该是。
编辑:事实上,克隆和拥有在这里并不实用。 GridView 依赖于其父类和同级类的不可访问部分,并且会导致导入至少 6000 行代码(AbsListView、AdapterView 等)
Like this previous person, I have unwanted overlap between GridView items:
Notice the text, in every column except the rightmost one.
Where I differ from that previous question is that I don't want a constant row height. I want the row height to vary to accommodate the tallest content in each row, for efficient use of screen space.
Looking at the source for GridView (not the authoritative copy, but kernel.org is still down), we can see in fillDown() and makeRow() that the last View seen is the "reference view": the row's height is set from the height of that View, not from the tallest one. This explains why the rightmost column is ok. Unfortunately, GridView is not well set-up for me to fix this by inheritance. All the relevant fields and methods are private.
So, before I take the well-worn bloaty path of "clone and own", is there a trick I'm missing here? I could use a TableLayout, but that would require me to implement numColumns="auto_fit"
myself (since I want e.g. just one long column on a phone screen), and it also wouldn't be an AdapterView, which this feels like it ought to be.
Edit: in fact, clone and own is not practical here. GridView depends on inaccessible parts of its parent and sibling classes, and would result in importing at least 6000 lines of code (AbsListView, AdapterView, etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我使用静态数组来驱动行的最大高度。这并不完美,因为在重新显示单元格之前,之前的列不会调整大小。这是膨胀的可重用内容视图的代码。
编辑:我正确地完成了这项工作,但我在渲染之前预先测量了所有单元格。我通过子类化 GridView 并在 onLayout 方法中添加测量钩子来做到这一点。
这是我的 BaseAdapter 子类中的测量函数。请注意,我有一个方法
updateItemDisplay
可以在视图单元格上设置所有适当的文本和图像。最后,这是设置为在布局期间测量视图单元格的 GridView 子类:
我将列数作为资源的原因是这样我可以根据方向等拥有不同的数字。
I used a static array to drive max heights for the row. This is not perfect since the earlier columns will not be resized until the cell is redisplayed. Here is the code for the inflated reusable content view.
Edit: I got this work correctly but I had pre-measure all cells before rendering. I did this by subclassing GridView and adding a measuring hook in the onLayout method.
Here is the measuring function in my BaseAdapter subclass. Note that I have a method
updateItemDisplay
that sets all appropriate text and images on the view cell.And finally, here is the GridView subclass set up to measure view cells during layout:
The reason I have the number of columns as a resource is so that I can have a different number based on orientation, etc.
根据 Chris 的信息,我在确定其他 GridView 项目的高度时使用了本机 GridView 使用的参考视图,从而使用了此解决方法。
我创建了这个 GridViewItemContainer 自定义类:
在适配器的 getView 方法中,将您的 convertView 包装为新的 GridViewItemContainer 中的子级,或者将其作为项目布局的顶部 XML 元素:
其中numColumns是GridView中的列数,'viewsInRow'是'position'所在当前行的View列表。
Based on the info from Chris, I used this workaround making use of the reference-View used by the native GridView when determining the height of other GridView items.
I created this GridViewItemContainer custom class:
In your adapter's getView method, either wrap your convertView as a child in a new GridViewItemContainer or make this one the top XML element of your item layout:
Where numColumns is the number of columns in the GridView and 'viewsInRow' is an list of View on the current row of where 'position' is located.
我做了很多研究,但发现了不完整的答案,或者很难理解解决方案的情况,但最终找到了一个与正确解释完美契合的答案。
我的问题是如何正确调整 gridview 项目的高度。当所有视图的高度相同时,此
网格视图
效果很好。但是,当您的视图具有不同的高度时,网格不会按预期运行。视图将相互重叠,形成一个美观的网格。这里解决方案我在XML布局中使用了这个类。
我使用了这个解决方案,效果非常好,非常感谢。--Abhishek Mittal
I did so many research but found incomplete answer or had tough with understanding what going on with solution but finally found an answer that fit perfectly with proper explanation.
My problem was to fit gridview item into height properly. This
Grid-view
worked great when all of your views are the same height. But when your views have different heights, the grid doesn't behave as expected. Views will overlap each other, causing anan-aesthetically
pleasing grid.Here Solution I used this class in XML layout.
I used this solution, and this is working very well, thanks a lot.--Abhishek Mittal
如果您将 GridView 或 ListView 转换为 RecyclerView,此问题将不会发生。而且您不需要创建自定义 GridView 类。
If you convert your GridView or ListView to a RecyclerView, this issue will not happen. And you won't need to make a custom GridView class.
这不是我下面提到的正确解决方案,但可以根据您的要求来解决。
只需从 gridview 的子布局中设置视图修复高度(在某些 dp 中,即 50dp),以便它可以被包裹。
This is not the correct solution which I am mentioned below, but can be workaround depends on your requirement.
Just set the height of view fix(in some dp i.e.- 50dp) from your child layout of gridview, so that it can be Wrapped.
为 GridView 赋予权重也适用于作为子项的 LinearLayouts 内的 GridView。这样,GridView 用它的子项填充视口,这样只要它们适合屏幕(然后滚动),您就可以查看它的项目。
但始终避免在 ScrollView 中使用 GridView。否则,您将需要计算每个孩子的身高并按照蔡斯上面的回答重新分配他们。
Giving weight to your GridView also works on GridViews inside LinearLayouts as a child. This way GridView fills the viewport with its children so you are able to view it's items as long as they fit the screen (then you scroll).
But always avoid using GridViews inside ScrollViews. Otherwise you will need to calculate each child's height and reassign them as Chase answered above.