android - DividerItemDecoration 没有出现在 RecyclerView 中

标签 android material-design android-recyclerview

我发现 DividerItemDecoration 可以很好地处理简单的单行列表项,但对于更复杂的项,它会拒绝显示。这是列表项的 XML:

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

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/imageView"
        android:transitionName="obrazek"
        android:layout_marginRight="5dp" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="?android:attr/selectableItemBackground">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/nazwa"
            android:maxLines="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/odleglosc"
            android:layout_gravity="right"
            android:gravity="end"
            android:layout_below="@+id/nazwa"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:clickable="false" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text=" "
            android:id="@+id/cena"
            android:layout_below="@+id/nazwa"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
        <!--
            <RatingBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ratingBar"
                android:layout_below="@+id/nazwa"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                style="@style/beerRatingBar" />
        -->

    </RelativeLayout>
</LinearLayout>

我正在为 RecyclerView 使用最常见的装饰器。就是不知道它为什么不想画。

recyclerView.addItemDecoration(new DividerItemDecoration(
                                getApplicationContext(), DividerItemDecoration.VERTICAL_LIST));

最佳答案

您的 RecyclerView 的方向是 android:orientation="horizo​​ntal",但您使用的是 DividerItemDecoration.VERTICAL_LIST。这是错误的。您应该使用 DividerItemDecoration.HORIZONTAL。这是违反直觉的:-)

关于android - DividerItemDecoration 没有出现在 RecyclerView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32542639/

相关文章:

java - Recyclerview - 从下到上重叠项目

android - 当 Recyclerview 不在第一项时如何禁用 swiperefreshlayout?

Android Studio/Eclipse 导入错误

angularjs - Angular Material : How to add multiple chips when comma separated string is added in the input?

java - 如何在 SharedPreferences 中保存我的应用程序背景?

android - 如何将 Coordinatorlayout 与 Linearlayout 一起使用?

android - BottomSheetDialog 上方的工具栏

android - 如何选择回收站 View 中的项目并将项目位置获取到我的 Activity

android - 如何在 Intellij 上重新生成 android 资源文件 (R.java)

java - 如何使用android studio解析recyclerview中的 'getData()'方法