首页 > 解决方案 > 为什么在 NestedScrollView 临时冻结中设置/更新 RecyclerView.Adapter?

问题描述

我有 NestedScrollView,它包含水平适配器和垂直适配器。当我设置适配器或更新适配器时,它总是暂时冻结(几秒钟 - 使用配置文件需要更长的时间)并且当添加更多项目时它会变得更糟!

这是 CPU 负载,但我不明白为什么会这样,我可以做得更好。

在此处输入图像描述

我的适配器代码很正常:

adapter = new Adapter(data);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapter);

知道可能出了什么问题或如何使这种层次结构更好吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@color/white_01">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomMenu"
        android:orientation="horizontal">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/white_01"
            android:orientation="vertical">

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/gray_05"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textBestSeller"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:maxLines="1"
                        android:paddingLeft="10dp"
                        android:paddingTop="10dp"
                        android:paddingRight="10dp"
                        android:textColor="@color/gray_01"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        tools:text="Nejprodávanější" />

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/rc1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white_01"
                    android:orientation="vertical">

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

                        <include
                            android:id="@+id/cardOrder"
                            layout="@layout/shared_card_two_lines"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1" />

                        <include
                            android:id="@+id/cardFilter"
                            layout="@layout/shared_card_two_lines"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingBottom="10dp">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Nalezeno 325 produktů"
                            android:textSize="12sp"
                            tools:text="Nalezeno 325 produktů" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:gravity="right"
                            android:text="Zrušit všechny filtry"
                            android:textAllCaps="true"
                            android:textColor="@color/blue_01"
                            android:textSize="12sp" />

                    </LinearLayout>

                </LinearLayout>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/rc2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>

    </LinearLayout>

    <include
        layout="@layout/loading_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"
        tools:visibility="visible" />

    <include
        layout="@layout/retry_layout"
        android:visibility="gone"
        tools:visibility="gone" />

    <include
        android:id="@+id/bottomMenu"
        layout="@layout/shared_bottom_menu"
        android:layout_width="match_parent"
        android:layout_height="@dimen/bottom_menu_height"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

注意1:第一个线性布局是有原因的,它是包含内容的“通用”视图。

注意 2:我正在尝试实现与 GearBest 在主页上的类似行为(向下滚动)。对他们来说,它似乎运作良好,所以它是可行的。

仍然冻结的适配器:

public class SomeAdapter extends RecyclerView.Adapter<SomeAdapter.ViewHolder> {

    private List<ProductItem> items;

    public SomeAdapter(List<ProductItem> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View itemView) {
            super(itemView);
        }
    }
}

更新:将片段简化为此后,问题仍然存在。似乎由于某种原因,NestedScrollView 内的膨胀布局和 RecyclerView 的组合导致了冻结。我继续寻找。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v4.widget.NestedScrollView>

标签: androidandroid-recyclerviewfreeze

解决方案


您的布局层次结构非常深,并且查看您的适配器,它什么都不做,可能需要很长时间,因为它必须测量所有内容。见https://developer.android.com/topic/performance/vitals/render#common-jank

该页面还告诉您如何记录和调试问题。

从你相当简单的布局来看,这是用许多LinearLayouts 来表达的,我认为你可以做得更好,ConstraintLayout另请参阅ConstraintLayout 的用法。如解决方案中所述。

再次查看您的布局时,我看到 aRelativeLayoutNeatedScrollViewa LinearLayout(从布局根目录中看到),这似乎没有任何作用。我建议你删除它们。(这是从您的屏幕截图来看,因为您提供的布局具有不同的结构。)


推荐阅读