【问题标题】:RecyclerView inside HorizontalScrollView not showing all itemsHorizo​​ntalScrollView 内的 RecyclerView 不显示所有项目
【发布时间】:2019-05-14 17:32:17
【问题描述】:

我在HorizontalScrollView 中有一个RecyclerView。我没有看到 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>

【问题讨论】:

  • 我不明白你想要什么,.....“我需要滚动列表,背景是回收,而不是回收”
  • @ghita 你找到解决办法了吗?我也遇到了同样的问题
  • 是的,我会根据回复编辑问题
  • 谢谢!我将 Horizo​​ntalScrollView 的子项更改为 RelativeLayout,它对我来说非常好

标签: android android-recyclerview horizontalscrollview


【解决方案1】:

HorizontalScrollView 的子代设为RelativeLayout 而不是LinearLayout

【讨论】:

  • 现在,孩子是recycleView。你建议将回收包装到RelativeLayout?
【解决方案2】:

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

<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>

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

 <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>

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

【讨论】:

    【解决方案3】:

    onBindViewHolder 仅用于“屏幕上可见”项目。如果项目总数为 7,屏幕只能显示 4,则一切正常。

    因此名称为“RecycleView”,它回收可见视图,您的RecycleView中总共只有4个视图

    这个我不明白你的意思!?

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

    所以,我需要一个解决方案来滚动带有背景的列表 列表,或使用 Horizo​​ntalScrollView 显示所有项目

    【讨论】:

      猜你喜欢
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 2018-06-20
      相关资源
      最近更新 更多