首页 > 解决方案 > 使用搜索栏过滤时嵌套滚动视图中的回收器视图导致滞后

问题描述

我正在处理这个视图,顶部有一个标题,下面有一个搜索栏,然后是一个嵌套滚动视图,其中包含一个线性布局,进一步包含 2 个回收器视图。布局 xml 文件可以在下面找到。

我面临的问题是,当我通过搜索栏过滤回收站视图中的项目时,呈现过滤结果时存在明显的滞后。我已经将问题定位到嵌套滚动视图,因为当我摆脱这个(只有 2 个回收器视图)时,滞后的问题完全消失了。但是,当然,随着嵌套滚动视图的消失,我不能再同时滚动 2 个回收器视图。

现在,每个回收器视图都有 2 个适配器,每个适配器都实现了处理过滤逻辑的 Filterable 接口。此外,我有一个视图模型类和一个片段类。

如果这个问题不能轻易解决,那么我认为最后的办法是摆脱嵌套滚动视图,只有一个包含两个列表的回收器视图,并将适配器的数量减少到一个。这会使逻辑变得非常复杂,所以我希望有另一种解决方法。

请让我知道你们的想法。任何输入表示赞赏。

<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:id="@+id/invite_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <include
        android:id="@+id/header"
        layout="@layout/component_bars_navigation_nav_sub"
        android:layout_width="match_parent"
        android:layout_height="@dimen/XXXL"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <include
        android:id="@+id/search_bar"
        layout="@layout/layout_component_bars_row_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/add_friend_header" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/invite_list_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:overScrollMode="always"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/search_bar">

        <LinearLayout
            android:id="@+id/invite_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/XXXXL"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="parent">

            <include
                android:id="@+id/label_1"
                layout="@layout/layout_component_bars_row_label"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                android:paddingBottom="@dimen/S"
                android:visibility="visible"
                tools:itemCount="3"
                tools:layout_editor_absoluteX="-50dp"
                tools:listitem="@layout/components_1_item"
                tools:visibility="visible" />

            <include
                android:id="@+id/label_2"
                layout="@layout/layout_component_bars_row_label"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                android:paddingBottom="@dimen/S"
                android:visibility="visible"
                tools:itemCount="2"
                tools:layout_editor_absoluteX="-50dp"
                tools:listitem="@layout/components_2_item"
                tools:visibility="visible" />

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidperformanceandroid-recyclerviewandroid-scrollview

解决方案


最后,我找到了解决这个问题的解决方案。我没有做多个回收视图,而是将它们组合成一个具有多种视图类型的视图。

如果人们仍然感到困惑,以下链接可能会有所帮助,因为它们帮助了我:)

具有多视图类型的Android RecyclerView(多视图持有者)

使用 RecyclerView 和多种视图类型


推荐阅读