带有自定义分隔线的GridLayoutManager [英] GridLayoutManager with custom divider

查看:926
本文介绍了带有自定义分隔线的GridLayoutManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用GridLayoutManagerRecyclerView中添加自定义分隔线,但没有成功,我进行了很多搜索,并调查了以下提及的答案,但对我没有帮助

I am trying to add custom divider in RecyclerView with GridLayoutManager but not getting success, i have searched a lot and looked into below mention answer but it didn't help me

链接1
链接2
链接3

link 1
link 2
link 3

我想在RecyclerView的每个项目之间插入黑线,如下所示.

I want to put black line in between each items of RecyclerView, something like below.

我在每行之间都有一条水平线,但也找不到如何在各列之间获得这些线.

I have got horizontal line in between each row, but not able to find how to get those lines in between columns also.

chintan soni的答案效果很好,但是当我有5个视图时,它仅在一种情况下产生了问题,它还显示了其他3个项目的分隔线,如下所示:

chintan soni's answer worked perfectly, but it is creating problem in one scenario only, when i am having 5 views, it shows divider of other 3 items also, like below :

推荐答案

检查一下: https ://bignerdranch.github.io/simple-item-decoration/

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

Add this to your app level gradle and sync:

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如下:

My line_divider.xml was as follows:

<?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>

这只是我的一个快速答案.但这应该可行,我猜..

This is just a quick answer from me. But this should work, I guess..

输出:

这篇关于带有自定义分隔线的GridLayoutManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆