- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用具有动态列号的 GridLayoutManager
在 RecyclerView
中呈现不同类型的项目。问题是,我有一个 RecyclerView.ItemDecoration
,它仅适用于 Type A
项目。此 RecyclerView.ItemDecoration
将边距添加到左侧列中那些项目的左侧/开始,并将边距添加到右侧列中那些项目的右侧/末尾。它基本上是为了让项目看起来更居中,所以拉伸(stretch)(这用于平板电脑/横向模式)。 RecyclerView
网格看起来像这样:
| A | | A |
| A | | A |
| B |
| A | | A |
| A | | A |
| B |
| A | | A |
ItemDecoration
看起来像这样:
class TabletGridSpaceItemDecoration(private val space: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) = with(outRect) {
val isTypeAItemLayout = view.findViewById<ConstraintLayout>(R.id.type_a_item_container) != null
if (isTypeAItemLayout) {
val adapterPosition = parent.getChildAdapterPosition(view)
if (adapterPosition % 2 == 0) {
left = space
right = 0
} else {
left = 0
right = space
}
}
}
}
这个装饰器的问题在于,在列表中的第一个 type B
项目之后,下一个 type A
项目的索引被搞砸了。因此,根据提供的示例,B
之后的第一个项目将具有 adapterPosition == 5
,因此根据 TabletGridSpaceItemDecoration
,marging 应添加到右侧,这是不正确的。
我尝试使用 HashMap
来保持 adapterPosition
和项目的真实位置,即忽略非 它在适配器上的位置>type A
项目。这还有一些其他问题,我不会详细介绍,但感觉这不是正确的方法。
我尝试的另一件事是检查将应用项目装饰的 View 在屏幕上的位置(更靠左或更靠右)。这样做的问题是当这个装饰器运行时 View 还没有被渲染。在 View 上添加 ViewTreeObserver.OnGlobalLayoutListener
是没有值(value)的,因为在呈现 View 时,项目装饰已经应用,这对 View 没有影响。
我想做的是检查一个项目是否在“第 0 列”或“第 1 列”中,并相应地添加边距。
我不知道这怎么可能,也没有找到方法查看 GridLayoutManager
提供的内容,可以通过 parent.layoutManager 作为 GridLayoutManager< 访问
.
有什么想法吗?谢谢
最佳答案
我分享这个作为答案,因为评论太长了。让我知道结果,如果不起作用,我会删除。
另外,很抱歉在 Java 中分享。我对 Kotlin 是文盲
您可以尝试使用 spanIndex
@Override
public void getItemOffsets(final Rect outRect, final View view, final RecyclerView parent, final State state) {
...
if(isTypeAItemLayout) {
int column = ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
if (column == 0) {
// First Column
} else {
// Second Column
}
}
}
更新。
对于 Kotlin :
val column: Int = (view.layoutParams as GridLayoutManager.LayoutParams).spanIndex
关于android - 获取 View 在 GridLayoutManager 上的列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56775671/
我正在使用 Recyclerview 和网格布局管理器在 android 中实现仪表板,其中连续 3 个项目。我将从服务器获取仪表板项目。根据项目的数量,我需要将Recyclerview项目调整到中心
我想知道是否有办法可以在android中自定义gridlayoutmanager以具有像这样的草图的水平布局: 我尝试了在 Github 中找到的一些布局管理器,但没有一个可以帮助我实现这种布局管理器
所以我使用 RecyclerView 和 GridLayoutManager 项目之间的间距在项目周围不一致 这是它在 4 x 4 网格中的样子 这是它在 5 x 5 网格中的样子 我已经尝试了很多来
我有一个 list ,假设有 20 个项目。我希望能够像这样加载它们 1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20 我正在使用 Recycler
我在使用 GridLayoutManager 填充回收器 View 时遇到问题。 我需要完成第一行,完成后,转到第二行。我做了一个重新排序列表的算法,所以它被正确显示,我可以在不做任何映射取消映射的情
我正在尝试使用 GridLayoutManager 在 RecyclerView 中添加自定义分隔线但没有成功,我已经搜索了很多并查看了下面提到的答案但它没有帮助我 link 1 link 2 lin
我在我的 RecyclerView 中使用 GridLayoutManager(GLM)。在 RecyclerView 中,我将填充 CardViews,如何设置我的 GLM 以仅呈现一个 CardV
我想在使用 RecyclerView 时在 GridLayoutManager 的一行中显示可变数量的列。要显示的列数取决于列的 TextView 的大小。 我不知道列宽,因为文本是动态放入其中的。
我正在试验 RecyclerView 和 GridLayoutManager。 我最终想要显示与这样显示相同大小的图像,如果可能的话甚至是正方形: GridLayoutManager 的元素是什么使这
我正在尝试使用 GridLayoutManager 构建一个 RecyclerView,它的每行列数可变,如下所示: 同一行中所有项目的宽度之和始终是屏幕宽度。 我尝试重新组织项目列表,按行列表对它们
我有一个带有显示卡片 View 的 GridLayoutManager 的 RecyclerView。我希望卡片根据屏幕大小重新排列(Google Play 应用程序使用它的应用程序卡片做这种事情)。
我有 3 个 Activity :A --> B --> C 在 Activity B 中,我使用 RecyclerView 的 GridlayoutManager 进行填充。我想在导航到 Activ
只是试图将我的 RecyclerView 水平居中。 这是xml: 这里是java代码: mRecyclerView = findViewById(R.id.recycler_view
在 GridLayoutManager 中将跨度大小设置为 1当LoadState是 Loading在分页库中。 我试过this解决方案,但它不起作用。 对于复制问题:克隆 this官方 repo 和
我需要具有水平滚动行为的 RecyclerView Gridlayout 管理器。它必须像垂直方向一样添加 subview (例如(第 1 行,第 1 列)= 索引 0,(第 1 行,第 2 列)=
我用了GridLayoutManager作为我的回收站 View 的布局管理器。 mRecyclerView.setLayoutManager(new GridLayoutManager(getC
我想在 RecyclerView 中创建类似的东西: 但是,我得到了这个: 有没有办法用 RecyclerView 实现这一点? 最佳答案 尝试并检查更换线路 postLine.setLayoutMa
我有 layoutManager = GridLayoutManager(context, 3) 如何使每条奇数线方向从左到右和偶数线 - 从右到左 看起来像: 0 -> 1 -> 2 5 7 ->
我有一个用 RecyclerView 填满屏幕的 fragment ( android:support:v7.widget.RecyclerView ) 和一个 GridLayoutManager .
我正在使用 GridLayoutManager,因此动态设置 recyclerview 的列数(每行)。这是我的代码: GridLayoutManager gridLayoutManager = ne
我是一名优秀的程序员,十分优秀!