首页 > 解决方案 > RecyclerView 滞后于 NestedScrollView

问题描述

我在主要活动中有一个底部应用程序栏,它只在nestedScrollView使用时隐藏,但是当一个带有 recyler 视图的片段被调用到 nestedScrollView 中时,它滞后了。

 android:nestedScrollingEnabled="false"

activity_main.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/backgroundcolor"
android:id="@+id/main_page"
android:layout_width="match_parent"

android:layout_height="match_parent">

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frag_replace"/>
<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar

        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:paddingTop="3dp"
        android:paddingBottom="3dp"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:tabMode="scrollable">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:elegantTextHeight="true"
            android:fontFamily="@font/pacifico_regular"
            android:text="@string/app_name"
            android:textColor="@color/colorDrawerBackground"
            android:textSize="25sp" />
    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottom_app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:backgroundTint="?attr/colorPrimary"
    app:fabAlignmentMode="center"
    app:hideOnScroll="true"
    app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
    app:navigationIcon="@drawable/ic_menu_black_24dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    style="@style/Widget.MaterialComponents.FloatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add_black_24dp"
    app:layout_anchor="@id/bottom_app_bar" />

片段布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.DailyList">

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

</FrameLayout>

这称为fragment

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.frag_replace, dailyList).commit();

还尝试recyclerview在java中设置嵌套滚动视图但仍然滞后

标签: androidandroid-studioandroid-layoutandroid-fragmentsandroid-recyclerview

解决方案


这应该有助于:ViewCompat.setNestedScrollingEnabled(recyclerView, false);

有关更多信息,请参阅本文: https ://android.jlelse.eu/recyclerview-within-nestedscrollview-scrolling-issue-3180b5ad2542


推荐阅读