【问题标题】:DividerItemDecoration doesn't show up in RecyclerViewDividerItemDecoration 没有出现在 RecyclerView 中
【发布时间】:2015-12-09 03:31:07
【问题描述】:

我发现 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));

【问题讨论】:

    标签: android material-design android-recyclerview


    【解决方案1】:

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

    【讨论】:

    • 我认为这是分隔线的方向,在垂直列表中是水平的。但无论如何,谢谢。
    【解决方案2】:

    A 也有类似的问题,但在我的情况下,它使用的是 RelativeLayout 作为我项目的根布局。将其更改为 LinearLayout 并且可以正常工作。

    我的建议是简化您的项目 xml,直到您可以看到分隔符。

    【讨论】:

    • 有一个相当简单的 RelativeLayout,我将其包裹在 LinerLayout 中 - 这没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多