【问题标题】:Divider line not showing in recycler view items回收站视图项目中未显示分隔线
【发布时间】:2017-09-17 22:56:31
【问题描述】:

在我的应用程序中,我正在使用一个包含许多项目的回收站视图,我想在项目之间显示一个点分隔符(分隔线),但它不起作用。我已经尝试创建一个可绘制的形状,但是在将可绘制对象添加到 DividerItemDecoration 之后,回收站视图项之间没有显示空间或线条。 我也尝试过创建自定义 DividerItemDecoration 类,但对我没有任何作用。 注意: 目前我的可绘制形状设置为矩形,我也尝试过线条。 如何实现。任何帮助将不胜感激。 这是我的代码。

可绘制:(customdrawableshape.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="3dp"
        android:height="2dp"
        android:color="#000000"
        android:dashGap="10dp"
        android:dashWidth="5dp" />

</shape>

回收站视图项的自定义行

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

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

以及我将项目装饰设置为 recyclerview 的代码部分

DividerItemDecoration dividerItemDecoration;
recyclerview.setLayoutManager(linearlayoutmanager);
dividerItemDecoration = new DividerItemDecoration(recyclerview.getContext(),
        linearlayoutmanager.getOrientation());
dividerItemDecoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.customdrawableshape));
recyclerview.addItemDecoration(dividerItemDecoration);

【问题讨论】:

标签: android android-recyclerview


【解决方案1】:

您可以使用DividerItemDecoration 类来添加行。

这里是示例代码

recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));

【讨论】:

  • 但我希望线条被宠爱,这就是我使用自定义形状的原因
  • 好的,那么你需要通过扩展 RecyclerView.ItemDecoration 类来创建自定义分隔线
  • 我在 DividerDecoration 上见过的最好的解决方案之一
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
相关资源
最近更新 更多