DividerItemDecoration 没有出现在 RecyclerView 中
DividerItemDecoration doesn't show up in 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));
A 有类似的问题,但在我的例子中,它使用 RelativeLayout 作为我项目的根布局。将其更改为 LinearLayout 并且有效。
我的建议是简化您的项目 xml 直到您可以看到分隔线。
您的 RecyclerView
方向是 android:orientation="horizontal"
,但您使用 DividerItemDecoration.VERTICAL_LIST
。这是错误的。你应该使用 DividerItemDecoration.HORIZONTAL
。这是违反直觉的:-)
我发现 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));
A 有类似的问题,但在我的例子中,它使用 RelativeLayout 作为我项目的根布局。将其更改为 LinearLayout 并且有效。
我的建议是简化您的项目 xml 直到您可以看到分隔线。
您的 RecyclerView
方向是 android:orientation="horizontal"
,但您使用 DividerItemDecoration.VERTICAL_LIST
。这是错误的。你应该使用 DividerItemDecoration.HORIZONTAL
。这是违反直觉的:-)