簡體   English   中英

API 27 到 29 遷移后 Android recyclerview 性能不佳

[英]Android recyclerview poor performance after API 27 to 29 Migration

遷移應用程序以支持最新的 API 29。一切進展順利,應用程序運行正常,除了一個令人困惑的問題; recyclerview使用 SQLite 查詢填充,該查詢可能檢索 500 多條記錄,並在如下所示的詳細活動中顯示結果。

在此處輸入圖片說明

這在 API 27 上一直運行良好,詳細活動幾乎立即打開和填充,而 sql 調用、活動代碼和布局在 5 年內沒有改變。 但是,在API 29版本上,使用相同的代碼,應用程序需要3+ 秒才能顯示相同的數據

經過大量測試和閱讀Slow Rendering ,我想我已經把它固定在 API 29 中的androidx.recyclerview:recyclerview:1.0.0花了更長的時間來構建活動 UI 的輸出,這可能是由於我糟糕的編碼和/或布局結構。

這是我的R.layout.msgh_detail由 .java 填充

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_collapseMode="pin" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="75dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <include
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/msgh_detail_list" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/replyCard"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="@dimen/card_margin"
    android:layout_marginRight="@dimen/card_margin"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    android:touchscreenBlocksFocus="false">

    <LinearLayout
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:maxLength="250"
            android:ems="10"
            android:imeOptions="actionDone"
            android:id="@+id/msg_reply"
            android:hint="@string/msg_reply_hint" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/replyCard"
        app:layout_anchorGravity="top|right|end"
        android:layout_margin="8dp"
        android:src="@drawable/ic_send_white_24dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

這是通過 .java ViewHolder包含的R.layout.msgh_detail_list_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="24dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_weight="0.97"
        android:id="@+id/DayMMdd"
        android:ellipsize="end"
        android:maxLines="1"
        android:hint="@string/day_mth_dte" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/arrivaltime"
        android:layout_marginLeft="8dp"
        android:hint="@string/Hour_min" />

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:id="@+id/avatar"
        android:src="@drawable/status_1"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:longClickable="false" />

</LinearLayout>


<androidx.cardview.widget.CardView
    android:id="@+id/detail_cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#ccecf9">

    <LinearLayout
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?attr/textAppearanceListItem"
            android:autoLink="web"
            android:textColorLink="#009933"
            android:layout_weight="0.97"
            android:text="@string/test_msg" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

顯然,這里發生了很多事情,坦率地說,我很驚訝它的效果如此之好,或者至少在 androidx 和 API 版本 29 出現之前它一直如此......!

問題:問題是該應用程序在 300 多個記錄集上變得非常緩慢,我對如何修復它感到有些困惑......

稍后修復!:在@martin-zeitler 提示后修訂了R.layout.msgh_detail現在似乎可以正常工作並幾乎立即加載數據。

刪除了整個androidx.core.widget.NestedScrollView塊,並從包含布局中替換為androidx.recyclerview.widget.RecyclerView (現已廢棄)。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_collapseMode="pin" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/detail_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="70dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/replyCard"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="@dimen/card_margin"
    android:layout_marginRight="@dimen/card_margin"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    android:touchscreenBlocksFocus="false">

    <LinearLayout
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:maxLength="250"
            android:ems="10"
            android:imeOptions="actionDone"
            android:id="@+id/msg_reply"
            android:hint="@string/msg_reply_hint" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/replyCard"
        app:layout_anchorGravity="top|right|end"
        android:layout_margin="8dp"
        android:src="@drawable/ic_send_white_24dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

還注意到有一次,它變得非常緩慢......

問題很可能是NestedScrollView > LinearLayout > RecyclerView

此外, include非常無用,只包含一個節點......你必須像這樣嵌套它:

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:isScrollContainer="true"
    android:longClickable="false"
    android:measureAllChildren="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        tools:listitem="@layout/cardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:color="?android:colorControlHighlight"
        android:fastScrollEnabled="true"
        android:gravity="start"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical"
        android:splitMotionEvents="false"
        android:verticalScrollbarPosition="right"/>

</androidx.core.widget.NestedScrollView>

暫無
暫無

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

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