首页 > 解决方案 > RecyclerView LayoutManager“一二一”作为Sololearn的关卡菜单

问题描述

我们如何制作这样的 LinearLayout 管理器:

在此处输入图像描述

我还检查了线性和网格管理器,但似乎在这种情况下我们需要一些新的东西。

标签: androidandroid-layoutandroid-recyclerview

解决方案


我在这个主题中发现了同样的问题,只是让它适合我的问题:

RecyclerView LayoutManager 不同行上的不同跨度计数

我们只需创建GridLayoutManager和覆盖方法,然后在 Recycler 中简单地设置它:

val layoutManager = GridLayoutManager(context, 2)
layoutManager.spanSizeLookup = object : SpanSizeLookup() {
    override fun getSpanSize(position: Int): Int {
        // 5 is the sum of items in one repeated section
        when (position % 3) {
            0 -> return 2
            1, 2 -> return 1
        }
        throw IllegalStateException("internal error")
    }
}

recycler.layoutManager = layoutManager

推荐阅读