RecyclerView LayoutManager different span counts on different rows

前端 未结 2 1818
时光取名叫无心
时光取名叫无心 2020-12-08 02:27

I want a RecyclerView.LayoutManager that allows me to specify different span counts for different rows as a repeating pattern. For example 2,3 with 10 items wou

2条回答
  •  感情败类
    2020-12-08 03:17

    here's how to do it in kotlin:

    val layoutManager= GridLayoutManager(activity, 3)
    layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
        override fun getSpanSize(position: Int): Int {
            return when (position) {
                0 -> 3
                else -> 1
            }
        }
    }
    recyclerView.layoutManager = layoutManager
    

    here, first we have created a grid layout manager with 3 columns, then we have specified the first will occupy the whole 3 columns while the rest take only one.

提交回复
热议问题