我试图在RecyclerView
添加自定义分隔符与GridLayoutManager
但没有获得成功,我搜索了很多,并查看下面提到的答案,但它没有帮助我
我想在RecyclerView
的每个项目之间放置黑线,如下所示。
我在每行之间都有水平线,但是也无法找到如何在列之间获得这些线。
chintan soni's答案工作得很好,但它仅在一个场景中创建问题,当我有5个视图时,它还显示其他3个项目的分隔符,如下所示:
看看这个: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>
这只是我的快速回答。但这应该有用,我想......
简单地说,用RecyclerView
在布局中编写你的XML文件在你的Activity中写下面的代码来实现GridLayoutManager
中RecyclerView
的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>
快乐的编码!
根据你的要求,我创建了四列。对于四列,这将有三条直线垂直线
<?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>