【问题标题】:Android. StaggeredGridLayoutManager determine span index安卓。 StaggeredGridLayoutManager 确定跨度索引
【发布时间】:2021-07-15 02:38:25
【问题描述】:

我使用recycleViewStaggeredGridLayoutManager(列数为3)。我需要确定当前单元格位于哪一列(按位置)。是否有可能做到这一点?请帮我。 注意:我的单元格的高度不同(正方形或矩形)

【问题讨论】:

    标签: android android-recyclerview staggeredgridlayoutmanager


    【解决方案1】:
    • 要从位置获取 :您可以使用 位置的余数/模数乘以 3。
    • 要从位置获取:您可以将其除以 3。
    0  | 1  | 2
    3  | 4  | 5
    6  | 7  | 8
    9  | 10 | 11
    12 | 13 | 14
    15 | 16 | 17
    18 | 19 | 20
    

    例子

    for (int position = 0; position <= 20; position++) {
        Log.d("LOG_TAG", "Item: " + position + " In Row: " + position / 3 + " In Col: " + position % 3);
    }
    

    日志:

    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 0 In Row: 0 In Col: 0
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 1 In Row: 0 In Col: 1
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 2 In Row: 0 In Col: 2
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 3 In Row: 1 In Col: 0
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 4 In Row: 1 In Col: 1
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 5 In Row: 1 In Col: 2
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 6 In Row: 2 In Col: 0
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 7 In Row: 2 In Col: 1
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 8 In Row: 2 In Col: 2
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 9 In Row: 3 In Col: 0
    2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 10 In Row: 3 In Col: 1
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 11 In Row: 3 In Col: 2
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 12 In Row: 4 In Col: 0
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 13 In Row: 4 In Col: 1
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 14 In Row: 4 In Col: 2
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 15 In Row: 5 In Col: 0
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 16 In Row: 5 In Col: 1
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 17 In Row: 5 In Col: 2
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 18 In Row: 6 In Col: 0
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 19 In Row: 6 In Col: 1
    2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 20 In Row: 6 In Col: 2
    

    【讨论】:

    • 感谢您的回答,但我的单元格可以有不同的高度(正方形或矩形)
    • @anna 我想说,如果不分享您如何设计这些正方形和矩形的高度,很难做到这一点
    【解决方案2】:

    我认为您可以使用子视图的LayoutParams 来确定跨度索引。带有ItemDecoration 的示例可能看起来像这样(Kotlin):

    class MyItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            super.getItemOffsets(outRect, view, parent, state)
            val params = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
            when(params.spanIndex) {
                0 -> outRect.apply {
                    left = 20
                    right = 10
                }
                1 -> outRect.apply {
                    left = 10
                    right = 10
                }
                2 -> outRect.apply {
                    left = 10
                    right = 20
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 1970-01-01
      • 2020-10-19
      • 2016-02-15
      • 2016-10-17
      • 2021-07-17
      • 1970-01-01
      • 2015-04-17
      相关资源
      最近更新 更多