偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何正確使用RecyclerView的setHasFixedSize方法提高布局計算性能

移動開發(fā) Android
在確定 RecyclerView? 的大小在整個生命周期中都不會改變時,才將 hasFixedSize()? 設(shè)置為 true。如果不確定,或者 RecyclerView? 的大小可能會改變,應(yīng)該將其設(shè)置為 false,確保 RecyclerView 能夠正確地重新計算其布局。

setHasFixedSize

setHasFixedSize(boolean hasFixedSize) 是 Android 中 RecyclerView 類的一個方法,用于設(shè)置 RecyclerView 是否具有固定大小。

RecyclerView源碼中setHasFixedSize方法的解釋:

/**
  * RecyclerView can perform several optimizations if it can know in advance that RecyclerView's
  * size is not affected by the adapter contents. RecyclerView can still change its size based
  * on other factors (e.g. its parent's size) but this size calculation cannot depend on the
  * size of its children or contents of its adapter (except the number of items in the adapter).
  * <p>
  * If your use of RecyclerView falls into this category, set this to {@code true}. It will allow
  * RecyclerView to avoid invalidating the whole layout when its adapter contents change.
  *
  * @param hasFixedSize true if adapter changes cannot affect the size of the RecyclerView.
*/
public void setHasFixedSize(boolean hasFixedSize) {
    mHasFixedSize = hasFixedSize;
}

翻譯一下注釋如下:

如果RecyclerView能夠提前知道RecyclerView的大小不受適配器內(nèi)容的影響,可以執(zhí)行幾個優(yōu)化。RecyclerView仍然可以根據(jù)其他因素(例如其父項的大?。└钠浯笮?,但此大小計算不能取決于其子項的大小或適配器的內(nèi)容(適配器中的項目數(shù)除外) 如果您對RecyclerView的使用屬于此類別,請將其設(shè)置為{@code true}。它將允許RecyclerView避免在適配器內(nèi)容更改時使整個布局無效。

@Override
protected void onMeasure(int widthSpec, int heightSpec) {
    if (mLayout == null) {
        defaultOnMeasure(widthSpec, heightSpec);
        return;
    }
    if (mLayout.isAutoMeasureEnabled()) {
        //....... 省略部分代碼
    } else {
        if (mHasFixedSize) {
            mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);
            return;
        }
        // custom onMeasure
        //......  省略部分代碼
        if (mAdapter != null) {
            mState.mItemCount = mAdapter.getItemCount();
        } else {
            mState.mItemCount = 0;
        }
        startInterceptRequestLayout();
        mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);
        stopInterceptRequestLayout(false);
        mState.mInPreLayout = false; // clear
    }
}

由上面內(nèi)容可知:調(diào)用 setHasFixedSize(true) 時,RecyclerView 的子項(items)的大小不會改變,即使添加或移除了 RecyclerView 中的項,RecyclerView 也不會重新測量和布局它的所有子項。好處是可以提高性能(測量和布局是一個相對耗時的操作)。

重要的是要確保RecyclerView 實際上具有固定大小。如果 RecyclerView 的子項大小可能會改變(例如,由于文本長度的變化或圖像加載),應(yīng)該調(diào)用 setHasFixedSize(false)。當(dāng)子項大小改變時,RecyclerView 會重新測量和布局它們確保能正確顯示。

如果你設(shè)置 hasFixedSize(true),但在運行時 RecyclerView 的大小實際上發(fā)生了變化(例如,因為其內(nèi)容或布局參數(shù)的變化),那么 RecyclerView 的布局可能不會正確地更新,可能會導(dǎo)致顯示問題。

總結(jié)

在確定 RecyclerView 的大小在整個生命周期中都不會改變時,才將 hasFixedSize() 設(shè)置為 true。如果不確定,或者 RecyclerView 的大小可能會改變,應(yīng)該將其設(shè)置為 false,確保 RecyclerView 能夠正確地重新計算其布局。

  1. 使用固定的寬度/高度(可以用setHasFixedSize(true)):
<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:overScrollMode="never"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="2" />
  1. 不使用固定的寬度/高度:應(yīng)該使用setHasFixedSize(false),因為寬度或高度可以改變RecyclerView的大小。
<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:overScrollMode="never"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="2" />

即使將 hasFixedSize() 設(shè)置為 true,RecyclerView 仍然會監(jiān)聽滾動事件,滾動性能不會受到影響,主要影響的是布局計算的性能。

責(zé)任編輯:武曉燕 來源: 沐雨花飛蝶
相關(guān)推薦

2016-09-22 09:24:33

AndroidViewStub

2013-01-08 16:05:23

Android開發(fā)布局ViewStub

2011-05-24 15:15:12

mysql性能

2017-07-10 13:09:45

前端Flexbox

2019-03-05 10:20:49

WebWebpack分離數(shù)據(jù)

2009-12-31 15:21:48

Silverlight

2011-04-27 16:38:31

投影機(jī)

2011-04-11 14:56:09

Oracle性能

2021-07-14 14:06:06

CSS前端瀏覽器

2009-11-02 14:08:05

2009-08-06 11:12:17

提高GDI編程性能

2010-01-05 18:49:57

.NET Framew

2015-10-10 11:00:05

RubyRails性能

2015-11-16 10:21:28

Java中鎖性能

2021-11-05 11:03:33

云計算開發(fā)技術(shù)

2015-03-18 09:59:14

CSSCSS提高渲染性

2010-07-29 10:19:18

提高DB2 IMPOR

2015-10-14 17:27:18

性能

2023-08-18 14:10:00

CDN數(shù)據(jù)中心

2019-05-22 14:55:57

云計算企業(yè)微服務(wù)
點贊
收藏

51CTO技術(shù)棧公眾號