簡體   English   中英

為什么在NestedScrollView中設置/更新RecyclerView.Adapter暫時凍結?

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

我有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>

您的布局層次結構很深,正在查看適配器,該適配器什么也不做,可能需要很長時間,因為它必須測量所有內容。 參見https://developer.android.com/topic/performance/vitals/render#common-jank

該頁面還告訴您如何記錄和調試問題。

從您使用許多 LinearLayouts表示的相當簡單的布局來看,我認為使用ConstraintLayout可以做得更好,還請參見ConstraintLayout的用法 解決方案中所述

再看一下您的布局,我看到一個RelativeLayout一個NeatedScrollViewLinearLayout (從布局根目錄看到)似乎NeatedScrollView 我建議您刪除它們。 (這是根據您的屏幕截圖判斷的,因為您提供的布局具有不同的結構。)

經過一些研究,唯一可行的解​​決方案是刪除NestedScrollView,並將所有內容放入一個回收器視圖中,並在適配器內部將它們作為不同類型的視圖支架進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM