RecyclerView项目占据整个屏幕

8
我有一个RecyclerView,在Android版本23上运行良好,但如果我在Android版本25上运行相同的代码,则整个屏幕都被单个项目占用。
最初,列表看起来很好,其中项高度为包装内容。但是,当我滚动时,整个屏幕被单个项目占用。
下面是包含RecyclerView的我的布局:
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:scrollbars="vertical" />

</LinearLayout>

2
请发布您的RecyclerView项XML。 - Dharmbir Singh
请发布您在RecyclerView中填充的项目布局。只有这样,我们才能更准确地帮助您。 - v4_adi
为你的充气布局高度设置wrap_content。 - Baskaran Veerabathiran
https://stackoverflow.com/a/48310052/6021740 - Kidus Tekeste
5个回答

10

您的Recyclerview代码没问题。 如果您在iteam_raw.xml中使用TextView或其他组件,请确保将其设置为“wrap_content”。

iteam_raw.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

  <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

5
请使用 RecyclerView 的项布局高度 "wrap_content"。

2

请确保在ViewHolder中使用的所有UI元素都具有包裹内容的高度,如果您使用cardview作为父容器,请将其高度也设置为wrap_content。


1
将线性布局中的 layout_height 属性从 match_parent 改为 wrap_content 或者设置一个具体的大小。
例如:
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="120dp" <-- **change in height**
    android:layout_width="match_parent"
    android:orientation="vertical">

0

将布局的高度和宽度设置为0dp

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:scrollbars="vertical" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接