android - RecyclerView notifyDataSetChanged() 在没有 ANR 的情况下卡住 UI

标签 android android-recyclerview android-anr-dialog android-nestedscrollview

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

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

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

我们试图删除其中一个回收器 View ,以删除 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>

最佳答案

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

我将 CollapsingToolbarLayout 放在 CoordinatorLayout 内的 AppBarLayout 内。 然后我将 RecyclerView header 放在 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>

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

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

关于android - RecyclerView notifyDataSetChanged() 在没有 ANR 的情况下卡住 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42715762/

相关文章:

android - 使用 DiffUtil ListAdapter 时 Recyclerview 没有动画

android - 我可以使用 ViewPager2 设置非线性(旧式)动画吗?

android - 通知 onSwiped 方法列表在 mainActivity 中排序

java - Android 在尝试读取文本或文档文件时没有响应

Android 应用程序,通过 UART 通过 BLE 模块接收微 Controller 发送的数据

android - android 6.0运行时需要哪些权限

android - onTouchListener 不工作

android - Quickblox 如何在 android 中记录通话?

android - 如何从 Activity 和 BroadcastReceiver 重现应用程序无响应 (ANR)

android - 调用Camera出现ANR错误如何解决?