【问题标题】:How do I scroll the screen with the RecyclerView inside with RecyclerView load items step by step?如何使用 RecyclerView 滚动屏幕并逐步加载 RecyclerView 项目?
【发布时间】:2020-02-24 10:55:03
【问题描述】:

我的项目中有画廊页面。此页面中有不同的视图类型,例如 RecyclerView - TextView - ImageView。如果我设置 RecyclerView 的高度“wrap_content”,我的 RecyclerView 会加载所有项目。这在我看来是冻结的,因为有些画廊有太多的项目。我想逐步加载项目。但是如果我设置 RecyclerView 的高度“match_constraint”,我的图库页面有 2 个滚动条(其中之一是 ScrollView,其中之一是 RecyclerView)。所以所有视图不能一起滚动。如何使用 RecyclerView 滚动屏幕并逐步加载 RecyclerView 项目?

其实我的 XML 太长了,所以我没有在下面的代码中添加一些视图。

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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:id="@+id/detail_fragment_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:fillViewport="true"
        android:fitsSystemWindows="false"
        android:focusable="true"
        tools:context=".DetailActivity">



 <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/detail_fragment_constraint"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:focusable="true"
            android:scrollbars="none">

<TextView
        android:id="@+id/feed_detail_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="12dp"
        android:paddingLeft="8dp"
        android:paddingTop="2dp"
        android:paddingRight="8dp"
        android:paddingBottom="2dp"
        android:textColor="@android:color/black"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent” />

<TextView
        android:id="@+id/feed_detail_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/feed_detail_category"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/feed_detail_category" />

<TextView
        android:id="@+id/feed_detail_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:paddingRight="8dp"
        android:paddingBottom="8dp"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/feed_detail_category" />

    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/comment_recycler_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/feed_detail_title” />         


 </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>

【问题讨论】:

  • 你有没有使用分页来逐步加载?对于两个滚动问题,您可以通过 yourRecyclerView.setNestedScrollingEnabled(false) 禁用回收器视图的滚动;在将其设置为适配器之前。
  • 如果我使用“yourRecyclerView.setNestedScrollingEnabled(false);”,我应该设置 RecyclerView 的高度“wrap_content”,因为我的 RecyclerView 无法滚动,并且我的 RecyclerView 在这种情况下会加载所有项目。
  • 我曾经在这个项目的不同视图中分页,但是在这种情况下我不能使用分页,因为当分页加载太多项目时,这也会导致它冻结。

标签: java android kotlin android-recyclerview scrollview


【解决方案1】:

使用来自 Android Jetpack 的分页库。 查看官方文档here

你也可以找到很好的教程Here

【讨论】:

    猜你喜欢
    • 2021-10-28
    • 2019-09-05
    • 2019-03-15
    • 2021-10-08
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多