在 2 行 Gridview 之间添加视图 [英] Add view between 2 rows Gridview

查看:16
本文介绍了在 2 行 Gridview 之间添加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView,每行有 3 个元素,当我单击一个项目时,该行下方会出现一个新视图.它有点像 iOS 上的文件夹应用程序.我没有在 SO 或 Google 上找到任何答案.也许你能给我一些提示.

I have a GridView, with 3 elements per row, and when I click on an item, a new view comes below the row. It's a bit like a folder application on iOS. I didn't find any answer on SO or on Google. Maybe you can give me some hints.

推荐答案

您可以使用 GridLayout 轻松完成此操作,但使用 GridView 则不行.

You can do this easily with GridLayout but not with GridView.

要在放置项目之前找出网格的可用宽度并设置列数,请设置 ViewTreeObserver.OnGlobalLayoutListener(然后可以放置您的项目)或扩展 GridLayout 并覆盖onMeasure (int widthMeasureSpec, int heightMeasureSpec).

To find out the available width of the grid before you place your items and set the number of columns, either set a ViewTreeObserver.OnGlobalLayoutListener (which can then place your items) or extend GridLayout and override onMeasure (int widthMeasureSpec, int heightMeasureSpec).

在每行内容后插入一行并将其可见性设置为 Visibility.GONE 并将 columnSpec 设置为 GridLayout.当用户点击一个项目时,您可以获得它的信息,填充它下面的视图,并展开或动画它的可见性切换.

Insert a row after every row of content and set it's visibility to Visibility.GONE and it's columnSpec to the number of columns of your GridLayout. When the user taps an item, you can get it's info, populate the view under it and expand or animate it's visibility toggling.

最后,对于指标,我只需将其添加为隐藏行的子项,当用户点击该项目时,计算所述项目的水平中心,并将此视图的中心在 X 轴上准确放置到该坐标(这种情况下边距是可以的).

Finally, for the indicator, I would just add it as a child of the hidden row and, when a user taps the item, calculate the horizontal center of said item and exactly place this view's center on the X axis to that coordinate (margins would be OK for this).

请注意,对于非常大的项目列表,不建议这样做,因为您必须实例化每个项目才能立即显示,无论它们是否都适合屏幕.与 GridView 不同,GridLayout 不是 AbsListView 的子元素.

Please note that for very large lists of items this is not recommended as you'll have to instantiate every item to display immediately, regardless if they all fit on the screen or not. Unlike GridView, GridLayout is not a child of AbsListView.

这篇关于在 2 行 Gridview 之间添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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