【问题标题】:item decoration not adding margin in item in Gridlayout manager recyclerview项目装饰未在 Gridlayout 管理器 recyclerview 中的项目中添加边距
【发布时间】:2021-08-01 13:59:37
【问题描述】:

我一直在尝试在网格布局管理器的 recyclerview 项目中使用项目装饰添加边距。

override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {

       outRect.set(10,10,10,10)
}

但它不会为项目增加边距 没有为网格项目添加边距的可能原因是什么?

【问题讨论】:

    标签: android android-gridlayout gridlayoutmanager item-decoration


    【解决方案1】:

    尝试像这样单独设置边距。

    @Override
      public void getItemOffsets(Rect outRect, View view, 
          RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space;
    
        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view) == 0) {
            outRect.top = space;
        } else {
            outRect.top = 0;
        }
      }
    

    还在recyclerView的XML中添加clipToPadding="false"

    【讨论】:

    • 您好,我试过单独设置边距,仍然没有添加边距。我正在使用嵌套的 recyclerview。
    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多