且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 2 行 Gridview 之间添加视图

更新时间:2023-12-06 14:09:28

您可以使用 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.