带有自定义分隔符的GridLayoutManager

时间:2016-09-14 12:10:45

标签: android android-recyclerview gridlayoutmanager

我正在尝试使用RecyclerViewGridLayoutManager中添加自定义分隔符,但未获得成功,我已经搜索了很多并查看了下面提到的答案,但它并没有帮助我

link 1
link 2
link 3

我想在RecyclerView的每个项目之间添加黑线,如下所示。

enter image description here

我在每行之间都有水平线,但是也无法找到如何在列之间获取这些线。

chintan soni's答案工作得很好,但它仅在一个场景中创建问题,当我有5个视图时,它还显示其他3个项目的分隔符,如下所示:

enter image description here

3 个答案:

答案 0 :(得分:7)

检查出来:https://bignerdranch.github.io/simple-item-decoration/

将此添加到您的应用级关卡并同步:

compile 'com.bignerdranch.android:simple-item-decoration:1.0.0'

然后,应用如下代码:

    Drawable horizontalDivider = ContextCompat.getDrawable(this, R.drawable.line_divider);
    Drawable verticalDivider = ContextCompat.getDrawable(this, R.drawable.line_divider);
    recyclerView.addItemDecoration(new GridDividerItemDecoration(horizontalDivider, verticalDivider, 4));

我的line_divider.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:width="1dp"
        android:height="1dp" />

    <solid android:color="@android:color/black" />

</shape>

这只是我的快速回答。但这应该有效,我想..

<强>输出: enter image description here

答案 1 :(得分:4)

enter image description here只需使用RecyclerView在布局中编写XML文件 在您的练习中,编写以下代码以实现GridLayoutManager

RecyclerView的除法器
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 3);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
DividerItemDecoration Hdivider = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL);
DividerItemDecoration Vdivider = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
Hdivider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.divider));
Vdivider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.divider));
recyclerView.addItemDecoration(Hdivider);
recyclerView.addItemDecoration(Vdivider);

上面,同时添加了水平和垂直分隔线,以获得整个网格外观。 Drawable文件的外观与您的应用程序完全一样。我的看起来像这样。

   <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <size
            android:width="1dp"
            android:height="1dp" />
        <solid android:color="@color/white" />
    </shape>

快乐编码!

答案 2 :(得分:0)

根据你的要求,我创建了四列。这将有三条直线垂直线为四列

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/black"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/black"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/black"/>
    </RelativeLayout>
</LinearLayout>