android-RecyclerView的GridLayoutManager动态跨度计数

android-RecyclerView的GridLayoutManager动态跨度计数,第1张

概述我正在使用以下代码动态更改跨度计数.vallayoutManager=GridLayoutManager(this,3)layoutManager.spanSizeLookup=object:GridLayoutManager.SpanSizeLookup(){overridefungetSpanSize(position:Int):Int{when(position){0,1,2->

我正在使用以下代码动态更改跨度计数.

val layoutManager = GrIDLayoutManager(this, 3)layoutManager.spanSizeLookup = object : GrIDLayoutManager.SpanSizeLookup() {    overrIDe fun getSpanSize(position: Int): Int {        when (position) {            0, 1, 2 -> return 1            3, 4 -> return 2            5 -> return 3            else -> return 1        }    }}

我得到了以下输出.
但是我希望D和E在水平方向上对齐.我不怎么做.

实际上,我在适配器中有3种类型:header,TYPE_A,TYPE_B.
header应该只有一行,TYPE_A是3行,TYPE_B是2行.

因此,请允许我获得帮助,以使某些列应具有1个1行,而某些列仅应具有2行(水平均等对齐),而另一些则具有3行.

解决方法:

在这种情况下,您应该使网格布局具有3个以上的单元.您需要选择一个对所有三种类型的单元格都适用的数字,最好是6,因为按行有3个单元格会返回2.要按行有2个单元格,您会返回3并具有每行1个单元格,您将返回6:

val layoutManager = GrIDLayoutManager(this, 6)layoutManager.spanSizeLookup = object : GrIDLayoutManager.SpanSizeLookup() {    overrIDe fun getSpanSize(position: Int): Int {        when (position) {            0, 1, 2 -> return 2            3, 4 -> return 3            5 -> return 6            else -> return 2        }    }}
总结

以上是内存溢出为你收集整理的android-RecyclerView的GridLayoutManager动态跨度计数全部内容,希望文章能够帮你解决android-RecyclerView的GridLayoutManager动态跨度计数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1092124.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存