RecyclerView layoutmanager for changing row and column span

后端 未结 2 472
执念已碎
执念已碎 2020-12-13 16:30

I am looking to create the following layout (red sqaures does not really have to be squares, a custom fixed height is also fine). The layout seems very simple yet I did not

2条回答
  •  一个人的身影
    2020-12-13 16:53

    Finally did it, it will help everyone who is trying to achieve any kind of patterns

    I was looking for the same result but with one header and lazy loading footer. I tried @Kristyna answers of Nick Butcher Library too. But it has many issues like fast scrolling view disappear problem with the extra space or fixed cell height issue.

    I found another library of Arasthel SpannedGridLayoutManager and tried to implement it. But it also has many issues like fixed height and footers extra space issue

    Finally, after 5hr of digging Arasthel's SpannedGridLayoutManager, I fixed the issues and found my result.

    My final edited forked library download from here SpannedGridLayoutManager.

    In my case

      val adapter = GridItemAdapter()//This is your adapter
      val spannedGridLayoutManager = SpannedGridLayoutManager(orientation = VERTICAL, spans = 3)
      spannedGridLayoutManager.itemOrderIsStable = true
      recyclerview.layoutManager = spannedGridLayoutManager
    

    And

      spannedGridLayoutManager.spanSizeLookup = SpannedGridLayoutManager.SpanSizeLookup { position ->
            when {
                position == 0 -> {
                    /**
                     * 150f is now static 
                     * should calculate programmatically in runtime
                     * for to manage header hight for different resolution devices
                     */
                    SpanSize(3, 1, 150f)
                }
                position % 7 == 1 ->
                    SpanSize(2, 2)
                else ->
                    SpanSize(1, 1)
            }
        }
      recyclerview.adapter = adapter
    

    This spanSizeLookup logic may vary according to the requirement, for me I had set it randomly for testing purposes.

提交回复
热议问题