带有自定义分隔符的GridLayoutManager

问题描述 投票:5回答:3

我试图在RecyclerView添加自定义分隔符与GridLayoutManager但没有获得成功,我搜索了很多,并查看下面提到的答案,但它没有帮助我

link 1 link 2 link 3

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

enter image description here

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

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

enter image description here

android android-recyclerview gridlayoutmanager
3个回答
8
投票

看看这个:https://bignerdranch.github.io/simple-item-decoration/

将此添加到您的应用级别gradle并同步:

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


4
投票

enter image description here简单地说,用RecyclerView在布局中编写你的XML文件在你的Activity中写下面的代码来实现GridLayoutManagerRecyclerView的divider

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>

快乐的编码!


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>
© www.soinside.com 2019 - 2024. All rights reserved.