【问题标题】:RecyclerView inside nested scrollview loads slow initially嵌套滚动视图中的 RecyclerView 最初加载缓慢
【发布时间】:2022-01-06 01:50:45
【问题描述】:

我有一个recyclerView,如下所示。这是在fragment 内,这是应用程序启动时显示的第一个屏幕。在我添加NestedScrollView 之前,recyclerView 用于快速加载数据(或recyclerView 立即显示)。现在,在添加NestedScrollView 之后,recyclerView 需要一些时间来加载数据(或显示 recyclerView)。

不知道是recyclerView显示晚了还是数据加载慢了。

注意:一旦显示,滚动时没有问题,项目在滚动时以正常速度加载。问题仅在初始加载时出现。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    android:background="@color/colorOffWhite"
    android:orientation="vertical"
    tools:context=".ui.fragments.HomeFragment">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="false"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/vp_deals"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:orientation="horizontal"
                    android:visibility="gone"
                    tools:visibility="visible"/>

                <TextView
                    android:id="@+id/tv_all_deals"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/colorWhite"
                    android:text="All Deals"
                    android:layout_gravity="bottom|right"
                    android:layout_marginEnd="5dp"
                    android:layout_marginBottom="3dp"
                    android:background="@drawable/app_gradient_color_background"
                    android:padding="3dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:foreground="?android:attr/selectableItemBackgroundBorderless"/>

            </FrameLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_home_items"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                android:focusableInTouchMode="true"/>

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</RelativeLayout>

【问题讨论】:

  • 触摸或滚动没有任何问题。
  • 如果你有 50 个项目要显示在这个 recyclerView 中,适配器中的 onCreateViewHolder() 方法将被调用 50 次,这是你的问题导致第一次启动速度慢
  • 请在 java/kotlin 文件中设置 nestedScrollEnabled = false
  • 我在xmlkotlin 中设置了它,但是没有用。
  • 谁能帮我解决这个问题?

标签: android android-recyclerview android-nestedscrollview


【解决方案1】:

当您尝试在 NestedScrollView 中创建回收站视图时,这是一个常见问题,导致回收站视图高度未定义

尝试设置recyclerview.setNestedScrollingEnabled(false);

我认为它会解决这个问题

另外,当recycler view放置在nestedScrollView中时,最好为recycler view定义一个高度

【讨论】:

  • 我在xml中有android:nestedScrollingEnabled="false"
  • 那你需要为recycler view定义一定的高度
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多