简体   繁体   中英

Why set/update RecyclerView.Adapter inside NestedScrollView temporary freezing?

I have NestedScrollView, which contains horizontal adapter and vertical adapter. When I set adapter or update adapter, it always temporary freeze (few seconds - with profile it takes longer) and it get worse when more items is added!

Here is CPU load, but I do not understand why it is happening, what I can do better.

在此处输入图片说明

My code for adapter is pretty normal:

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

Any idea what could be wrong or how make this hierarchy better?

<?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>

Note1: First linear layout has reason, it is "generic" view which contains contents.

Note2: I am trying achive similar behaviour as GearBest has on homepage (scroll down). For them it seem to work well, so it is doable.

Adapter which still freezing:

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);
        }
    }
}

UPDATE: After simplify fragment to this, problem is still there. It seems that for some reason combination of inflated layout and RecyclerView inside NestedScrollView cause the freeze. I continue searching.

<?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>

Your layout hierarchy is pretty deep, and looking at your adapter, which does nothing, probably it takes long, because it has to measure everything. See https://developer.android.com/topic/performance/vitals/render#common-jank

The page also tells you how to record and debug the problem.

Judging from your fairly simple layout, that is expressed with many LinearLayouts I think you can do better using ConstraintLayout also see usage of ConstraintLayout . As stated in solutions .

On a second look at your layout I see a RelativeLayout a NeatedScrollView and a LinearLayout (seen from the layouts root), that seem to serve no purpose. I suggest you delete them. (This is judging from your screenshot, as the layout you provided has a different structure.)

经过一些研究,唯一可行的解​​决方案是删除NestedScrollView,并将所有内容放入一个回收器视图中,并在适配器内部将它们作为不同类型的视图支架进行处理。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM