RecyclerView 筆記

隨著listview的代碼因?yàn)樘砑觙eature而變得不可維護(hù),google開(kāi)始創(chuàng)建RecyclerView

ListView的缺點(diǎn):

  • 重復(fù)的功能
  • 動(dòng)畫(huà)不方便實(shí)現(xiàn)

RecyclerView 基于幾大組件實(shí)現(xiàn),代碼組織更加合理。

image.png

LayoutManager

  • 排布item在屏幕上的位置
  • handle the scroll
  • Focus Traversal
  • Accessibility

Adapter

  • create view and view holder
  • bind an item to a viewholder
  • notify recyclerView about changes
  • item interaction handling (click etc)
  • multiple view type
  • Recycler recovery(onFailedToRecycleView)
  • Granular data change events

ViewHolder

  • viewHolder 的創(chuàng)建流程:
image.png
  • bind viewholder
image.png
  • view 的展示
image.png
  • view 的回收
image.png

注意:回收的時(shí)候先將viewholder放入cache中,這樣下次如果再展示這個(gè)position的view,可以直接從cache中取,不用再跟adapter打交道。cache中evict的老的數(shù)據(jù)再放入recycle pool中

image.png

如果view從屏幕上去除時(shí),需要做刪除動(dòng)畫(huà),則會(huì)被RecycleView臨時(shí)加入到viewGroup中,LayoutManager并不知道。然后由ItemAnimator進(jìn)行動(dòng)畫(huà)操作,動(dòng)畫(huà)完成后,才對(duì)view進(jìn)行回收

image.png

如果在回收時(shí),發(fā)現(xiàn)view正處于transient state(正在動(dòng)畫(huà),或者edit text中的內(nèi)容被選中),會(huì)導(dǎo)致recycle pool無(wú)法回收,這時(shí)會(huì)調(diào)用adapter的onFailedToRecycle,給adapter最后一次機(jī)會(huì)去清除transient state,以使view可以被回收,adpater可以清除動(dòng)畫(huà),并返回true,也可以返回false,這樣viewholder會(huì)被銷(xiāo)毀

image.png

還有一種情況會(huì)導(dǎo)致viewholder銷(xiāo)毀,recycle pool對(duì)于每種type的view,只能回收一定的數(shù)量,一旦超過(guò)這個(gè)數(shù)量,多余的就會(huì)被銷(xiāo)毀。

image.png
image.png

ChildHelper

因?yàn)閘ayoutManager可能會(huì)和ItemAnimator對(duì)一個(gè)view產(chǎn)生沖突的操作(remove view vs animation),引入childHelper為layoutmaanger提供一個(gè)虛擬的視圖,等itemAnimator的動(dòng)畫(huà)完成后才進(jìn)行真正view的操作

image.png

Adapter Helper

Adapter Helper記錄adapter上的notify操作,并對(duì)notify操作進(jìn)行reorder

image.png

ItemDecoration

  • 對(duì)每個(gè)子view添加decoration
  • add offset to view bound
image.png

RecycledViewPool

image.png

ItemTouchHelper

方便的實(shí)現(xiàn)drag&drop, swipe

Extra:

  • 如果數(shù)據(jù)沒(méi)有更新,則不會(huì)重新bindViewHolder
image.png
  • onBindViewHolder中傳入position不一定是最終在adapter中的位置,因?yàn)閍dapter中數(shù)據(jù)改變了,不一定馬上反映到view的位置上
image.png
  • change with payload

當(dāng)notifyItemChange時(shí)傳入了payload,當(dāng)前position會(huì)使用同一viewholder,只用部分綁定就可以了,如果沒(méi)有payload則

image.png
image.png

不要在create里面返回同一個(gè)view

image.png

adapter position 有可能與layout position不一致until next layout

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、概述 對(duì)于RecyclerView的學(xué)習(xí),主要是需要掌握以下幾點(diǎn): 數(shù)據(jù):Adapter 使用:Recycle...
    澤毛閱讀 7,607評(píng)論 1 23
  • 參考:這是一篇Android RecyclerView使用介紹哦(好?。?本文的源代碼:RecyclerViews...
    NickelFox閱讀 684評(píng)論 0 1
  • layout: posttitle: "新控件筆記"date: 2016-08-16 12:16:58 +0800...
    qianxL閱讀 680評(píng)論 0 0
  • 這篇文章分三個(gè)部分,簡(jiǎn)單跟大家講一下 RecyclerView 的常用方法與奇葩用法;工作原理與ListView比...
    LucasAdam閱讀 4,698評(píng)論 0 27
  • 正如一位明星不可能獲得所有人的追捧一樣,一個(gè)創(chuàng)業(yè)項(xiàng)目也不可能適合所有人參與。俗話說(shuō):請(qǐng)神容易送神難。創(chuàng)業(yè)者在尋找合...
    云華商業(yè)頻道閱讀 911評(píng)論 1 3

友情鏈接更多精彩內(nèi)容