Android RecyclerView notifyDataSetChanged()冻结没有ANR的UI

Android RecyclerView notifyDataSetChanged()冻结没有ANR的UI,android,android-recyclerview,android-anr-dialog,android-nestedscrollview,Android,Android Recyclerview,Android Anr Dialog,Android Nestedscrollview,我们有一个NestedScrollView,它包含两个不同的RecyclerView视图,都使用垂直滚动。滚动布局位于SwipeRefreshLayout内部 Upd:我们知道getItemViewType(pos)方法,并在其他地方使用它。但在这里,我们有复杂的逻辑,将进一步分为两个不同的屏幕,因此我们为每个RecyclerView有两个单独的演示者,将它们组合成一个,并在一个月内再次分离不是一个选项 从某个时刻开始,我们注意到使用滑动刷新更新屏幕开始冻结UI:日志中跳过的帧,无法对UI进行任

我们有一个NestedScrollView,它包含两个不同的
RecyclerView
视图,都使用垂直滚动。滚动布局位于
SwipeRefreshLayout
内部

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

从某个时刻开始,我们注意到使用滑动刷新更新屏幕开始冻结UI:日志中跳过的帧,无法对UI进行任何操作,冻结进度条。这需要5秒钟,但我们没有得到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>

它看起来像
NestedScrollView
layout\u height=“wrap\u content”
一起打破了所有子级的优化
RecyclerViews
。无论父视图(
SwipeRefreshLayout
)中具有
layout\u height=“match\u parent”
的约束如何,它都为它们提供了无限的空间。因此,
RecyclerView
一次为适配器中的所有型号创建视图保持架。日志显示,每行需要20-60毫秒,这会给100行带来2-5秒的延迟


我们在onCreateViewHolder()中添加了日志记录,并注意到有几十个(几乎一百个)调用它,而屏幕上只有大约十个元素。将
NestedScrollView
layout\u height
更改为
“匹配父对象”
修复了此问题。

为什么要添加NestedScrollView和linearLayout以及2个recyclerViews

所有这些都可以通过swipeRefreshLayout和RecyclerView实现

尝试查看如何根据位置或数据类型添加不同的视图。因此,从技术上讲,您将只有一个recyclerView和一个适配器,但适配器将处理这两种情况(朋友和聊天),并将数据附加到视图(recyclerView)


由于RecyclerView还扩展了NestedScrollView,因此在NestedScrollView中保留RecyclerView将使您的滚动抖动,因此您必须添加isScrollContainer=true等标志。所以不要这样做

我也在努力解决这个问题,最后我想到我不能在nestedscrollView中正确使用RecyclerView和notifyData,所以我这样做:

我将折叠工具栏布局放在AppBarLayout内CoordinatorLayout内。 然后我将RecyclerView标题放在CollasingToolbarLayout中,将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\u behavior=“@string/appbar\u scrolling\u view\u behavior” 它应该在recyclerview容器布局中非常重要


请告诉我这对你是否有用。最好的

我们在需要的地方使用getItemViewType(),但这里我们希望能够轻松地将这两个视图分离到不同的屏幕中。它们有不同的适配器、演示者(根据MVP模式)等等。我们将很快将此屏幕划分为更小的屏幕,因此这就是使用此解决方案的原因。将NestedScrollView的布局高度更改为“匹配父屏幕”不会解决问题。谢谢