【问题标题】:Android - RecyclerView spacing between items in a GridAndroid - 网格中项目之间的 RecyclerView 间距
【发布时间】:2015-10-08 02:45:43
【问题描述】:

在我的 Android 应用程序中,我使用 RecyclerView 在网格中显示项目,方法是使用 GridLayoutManager。 在 GridView 中,为了指定元素之间的间距,我会设置 horizo​​ntalSpacingverticalSpacing 属性。

那么,我怎样才能在 RecyclerView 上做同样的事情呢?

【问题讨论】:

标签: android gridview android-recyclerview android-gridview


【解决方案1】:

我还没有使用 GridLayout 对其进行测试,但对于 LinearLayout,我只是为每个列表项的根布局设置了一个边距。我想如果你想要所有项目之间的空间相同(比如说 8dp),你必须这样做:

  1. 将 layout_padding 4 dp 设置为 RecyclerView。
  2. 为每个列表项设置 layout_margin 4 dp。

这样你将在每个项目周围有一个恒定的 8 dp。


下面的代码是一个水平的 RecyclerView,它以 8 dp 的正确间距显示图像:

<!-- fragment_layout.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        tools:listitem="@layout/list_item" />

</LinearLayout>

<!-- list_item.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_margin="4dp">

    <ImageView
        android:layout_width="@dimen/thumbnail_size"
        android:layout_height="@dimen/thumbnail_size"
        android:contentDescription="@string/image_content_desc" />

</LinearLayout>

编辑:我意识到项目视图需要有一个父 ViewGroup,所以我更新了 sn-p。

【讨论】:

    【解决方案2】:

    您必须为此使用ItemDecorator

    像这样:

    public class EqualSpaceItemDecoration extends RecyclerView.ItemDecoration {
    
        private final int mSpaceHeight;
    
        public EqualSpaceItemDecoration(int mSpaceHeight) {
            this.mSpaceHeight = mSpaceHeight;
        }
    
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                                   RecyclerView.State state) {
            outRect.bottom = mSpaceHeight;
            outRect.top = mSpaceHeight;
            outRect.left = mSpaceHeight;
            outRect.right = mSpaceHeight;
        }
    }
    

    【讨论】:

    • 您复制/粘贴答案并没有对“horizo​​ntalSpacing 和 verticalSpacing”说什么:)
    • top+bottom=vertical, left+right+horizo​​ntal spacing ,我觉得很简单所以没提,它的名字也暗示它是 equalSpace 意味着无论你在它垂直和水平传递间距将相同
    • 我读了这篇文章“stackoverflow.com/questions/24618829/…”,你的解决方案不能只应用两列或更多列之间的空间,这个解决方案是在单个对象上做边距,而不是垂直或水平的列! ,注意:存在一个没有 4 行作为示例的电源库。 github.com/lucasr/twoway-view
    • 又好又简单,tnx
    【解决方案3】:
    public class EqualSpaceItemDecoration extends RecyclerView.ItemDecoration {
    
    private final int mSpaceHeight;
    
    public EqualSpaceItemDecoration(int mSpaceHeight) {
        this.mSpaceHeight = mSpaceHeight;
    }
    
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        outRect.bottom = mSpaceHeight;
    outRect.top = mSpaceHeight;
    outRect.left = mSpaceHeight;
    outRect.right = mSpaceHeight;
    }
    

    上述答案将在图像相遇处产生不相等的边距宽度。

    以下解决方案产生均匀的边距


     public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {        
            int position= parent.getChildAdapterPosition (view);
            int column = position% numberOfColumns;       
                outRect.left= margin-column*spacingPx/numberOfColumns;
                outRect.right= (column+1)* margin/numberOfColumns;
                outRect.top= margin;           
    
            }
    
        }
    

    【讨论】:

    • 只是一个旁注,答案的位置不是静态的。
    • 请详细说明
    • 如果有人赞成您的解决方案,它最终可能会被置于层次结构中的较高位置,因此“上述答案”毫无意义,因为它假定了特定的顺序。
    猜你喜欢
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2017-01-04
    相关资源
    最近更新 更多