RecyclerView notifyDataSetChanged() 在没有 ANR 的情况下冻结 UI

Gak*_*ket 2 android android-anr-dialog android-recyclerview android-nestedscrollview

我们有一个 NestedScrollView,它包含两个不同的RecyclerView垂直滚动。滚动布局位于SwipeRefreshLayout.

更新:我们知道getItemViewType(pos)方法并在其他地方使用它。但是这里我们有复杂的逻辑,将进一步分为 2 个不同的屏幕,因此我们为每个 RecyclerView 设置了两个单独的演示器,将它们合并为一个并在一个月内再次分离不是一种选择。

从某个时候,我们注意到使用滑动刷新更新屏幕开始冻结 UI:日志中的跳帧、无法使用 UI 进行任何操作、冻结进度条。最多需要五秒钟,但我们没有收到 ANR。它在开始时运行良好,在结束时冻结。如果我们notifyDataSetChanged()在适配器中移除,一切看起来都不错。

我们试图删除回收站视图之一,从ViewHolder.onCreate()和 中删除所有操作,onBindViewHolder()但没有帮助。此外,我们交叉检查了问题是否来自某些数据处理(我们使用 RxJava 2 来操作线程),我们看到所有与网络、数据库和数据处理相关的操作都是在非 UI 线程中进行的,并且目前已经完成当滞后开始时。

这是布局:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height=«match_parent"
    android:orientation="vertical"
    >
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content»
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation=«vertical"
        android:paddingBottom="@dimen/spacing_bigger»>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycle_chats"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white_bg"
            />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycle_friends"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white_bg"
            />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

iDe*_*per 5

我也在为此苦苦挣扎,终于想到了我不能在nestedscrollView中正确使用RecyclerView WITH notifyData 所以我这样做的想法:

我把 CollapsingToolbarLayout 放在 AppBarLayout 里面 CoordinatorLayout 。然后我把 RecyclerView 头放在 CollapsingToolbarLayout 里面,把 RecyclerView 放在 AppBarLayout 下面的 CoordinatorLayout 里面。

并且我还使用了 RecyclerView 的 addOnScrollListener 方法在分页后为 RecyclerView 加载新项目。

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="#ffffff">

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

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/activity_txt_news_detail.app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00ffffff"
            app:elevation="0dp">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:expandedTitleMarginStart="64dp"
                app:layout_scrollFlags="scroll|snap">


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

                    // all other views
                    // as header of
                    // recyclerview comes here
                </LinearLayout>

                <!--todo tgs : here top-->


            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
             app:layout_behavior="@string/
             appbar_scrolling_view_behavior">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/home_news_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:listitem="@layout/row_news_txt_small" />

        </RelativeLayout>


    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

这行代码 app:layout_behavior="@string/appbar_scrolling_view_behavior" 很重要,应该在 recyclerview 容器布局中

如果对你有用,请告诉我。祝一切顺利