【问题标题】:GridLayoutManager with custom divider带有自定义分隔线的 GridLayoutManager
【发布时间】:2016-09-14 12:10:45
【问题描述】:

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

link 1
link 2
link 3

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

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

chintan soni's 回答完美,但它只在一种情况下产生问题,当我有 5 个视图时,它还显示其他 3 个项目的分隔符,如下所示:

【问题讨论】:

  • 这个答案工作正常。 stackoverflow.com/a/29168276/2900893
  • @ShabbirDhangot 它只是在项目之间留出空间,不允许设置自定义分隔线,我已经尝试过了。在那个答案中,我还没有看到任何设置颜色的范围,否则我也可以使用这种方式
  • 你是如何得到行之间的自定义分隔符的?
  • 通过使用this 回答。
  • 垂直线可以做一些简单的修改。创建 2dp 宽度和 match_parent 高度的视图。现在你将这个视图的重力设置为 centerVerticlae。这将在屏幕上显示直线。现在让 recyclerview 背景透明,并在列之间留出一些空间来显示它后面的视图。

标签: android android-recyclerview gridlayoutmanager


【解决方案1】:

看看这个: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>

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

输出:

【讨论】:

  • 这是终生正确的解决方案:)
  • @Piyush 是的.. 它是.. :)
  • @ChintanSoni 我这里有一个问题,如果我只有 5 个视图,第一行将包含 4 个视图,最后一个视图将在第二行,当时它仍然显示其他 3 个分隔符视图,实际上不存在。我已经更新了我的问题,你能检查一下吗?
  • 我收到 java.lang.NullPointerException:尝试在 com.zoho.desk.radar.maincard 的空对象引用上调用虚拟方法“int android.view.View.getTop()” .traffic.channel.adapter.GridDividerItemDecoration.drawHorizo​​ntalDividers(GridDividerItemDecoration.java:96) 在 com.zoho.desk.radar.maincard.traffic.channel.adapter.GridDividerItemDecoration.onDraw(GridDividerItemDecoration.java:44)
【解决方案2】:

简单地说,用RecyclerView在布局中编写你的XML文件 在您的活动中编写以下代码以在RecyclerView 中实现GridLayoutManager 的分隔符

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>

编码愉快!

【讨论】:

  • DividerItemDecoration.HORIZONTAL 分隔线在我的情况下不可见:(
【解决方案3】:

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

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

【讨论】:

  • 老而有名的终身解决方案.. :D
  • :) 会尝试,希望它会工作。但我有一个问题,如果我有 9 个项目怎么办?前 2 行将被填充,在第 3 行我会得到一个项目,但它仍然会显示 2 行第 3 列和第 4 列对吗?
  • 是的,这个和物品装饰器也会发生。它在 Item decorator 上显示空白部分。因为它是单色的,所以你不会看到任何副作用。
猜你喜欢
  • 1970-01-01
  • 2012-05-08
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多