LayoutManager for RecyclerView Grid with different cell width

后端 未结 3 1621
半阙折子戏
半阙折子戏 2020-12-04 07:34

StaggeredGridLayoutManager doesn\'t seem to allow customising a cell width or span multiple columns (except full span) for vertical orientation

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 08:26

    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
    

    For the first image result

      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
    

    And the second image result

     spannedGridLayoutManager.spanSizeLookup = SpannedGridLayoutManager.SpanSizeLookup { position ->
            when (position % 8) {
                0, 5 ->
                    SpanSize(2, 2)
                3, 7 ->
                    SpanSize(3, 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.

提交回复
热议问题