Horizo​​ntalScrollView 中的 RecyclerView 未显示所有项目

ghi*_*ita 3 android horizontalscrollview android-recyclerview

我有一个RecyclerView里面的HorizontalScrollView. 我没有看到RecyclerView所有项目的内部。我看过了,即使适配器中的列表有 7 个项目,onBindViewHolder也只调用了 4 次!如果我取出HorizontalScrollView,它工作正常。

我使用 是HorizontalScrollView因为我需要在回收的背景下滚动列表,而不是在回收内,它通常是如何工作的。

在此处输入图片说明

所以,我需要一个解决方案来滚动带有列表背景的列表,或者使用 HorizontalScrollView

更新 :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:scrollbars="none"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/label">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/rlWrapper"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@id/paddingStartView"
                android:background="@drawable/bg_round_corner">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/optionsRv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintStart_toStartOf="parent" />

            </RelativeLayout>

            <View
                android:id="@+id/paddingStartView"
                android:layout_width="16dp"
                android:layout_height="16dp" />

            <View
                android:id="@+id/paddingEndView"
                android:layout_width="16dp"
                android:layout_height="16dp"
                android:layout_toEndOf="@id/rlWrapper" />

        </RelativeLayout>


    </HorizontalScrollView>

    <TextView
        android:id="@+id/label"
        style="@style/FontLocalizedMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:textAllCaps="true"
        android:textColor="#979797"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Tempareature" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

小智 18

使孩子HorizontalScrollView成为RelativeLayout而不是LinearLayout


小智 12

我也刚刚遇到了这个问题,接受的答案很有帮助。我有:

<HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:fillViewport="true">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dates_recycler"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</HorizontalScrollView>
Run Code Online (Sandbox Code Playgroud)

并且它没有在水平方向的回收器视图中显示所有项目。它只显示了足够多的项目,适合设备视图的宽度,onBindViewHolder多次调用,等等。RelativeLayout在 和 之间添加一个 inHorizontalScrollViewRecyclerView修复它,以便它显示所有项目,无论它们是否全部适合设备的宽度,或不,您可以滚动找到它们。

 <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/dates_recycler"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </androidx.recyclerview.widget.RecyclerView>

        </RelativeLayout>

    </HorizontalScrollView>
Run Code Online (Sandbox Code Playgroud)

认为android:layout_width="match_parent"将设为 也是RelativeLayout关键。