1.RecyclerView详解
RecyclerView详解
RecyclerViewä½ä¸ºListViewåGridViewçæ¿ä»£ï¼ä½æ¯åListViewä¸ä¸æ ·çæ¯ï¼RecyclerViewä¸åè´è´£Itemçææ¾çæ¾ç¤ºæ¹é¢çåè½ï¼ææåå¸å±ãç»å¶çæ¹é¢çå·¥ä½é½æåæä¸åçç±»è¿è¡ç®¡çã
RecyclerViewä¸ListViewçä¸åç¹ï¼ä¸»è¦å¨äºä»¥ä¸å 个ç¹æ§ï¼
å¦æä½ æ³ä½¿ç¨RecyclerViewï¼éè¦å以ä¸æä½ï¼
æ们å¯ä»¥ä»ä¸å¾æ´ç´è§çäºè§£å°RecyclerViewçåºæ¬ç»æï¼
RecyclerViewä¸åè´è´£Itemè§å¾çå¸å±åæ¾ç¤ºï¼æ以RecyclerViewä¹æ²¡æ为Itemå¼æ¾OnItemClickçç¹å»äºä»¶ãå¯ä»¥éè¿ä»¥ä¸æ¹å¼è¿è¡ï¼å 为å¨ViewHolderæ们å¯ä»¥æ¿å°æ¯ä¸ªItemçæ ¹å¸å±ï¼æ以å¦ææä»¬ä¸ºæ ¹å¸å±è®¾ç½®åç¬çOnClickçå¬å¹¶å°å ¶å¼æ¾ç»Adapterï¼é£ä¹å°±å¯ä»¥å¨ç»è£ RecyclerViewæ¶å°±è½å¤è®¾ç½®ItemClickListenerï¼åªä¸è¿è¿ä¸ªListenerä¸æ¯è®¾ç½®å°RecyclerViewä¸èæ¯è®¾ç½®å°Adapterã
å¤Itemå¸å±ï¼getItemViewTypeæ¹æ³ï¼ç¨æ³åListView没æä»»ä½åºå«ï¼è¿éè¦æ³¨æçæ¯å½æ°onCreateViewHolder(ViewGroup parent,源码网站漏洞源码 int viewType)ç第äºä¸ªåæ°å°±æ¯Viewçç±»åï¼å¯ä»¥æ ¹æ®è¿ä¸ªç±»åå¤æå»å建ä¸åitemçViewHolderï¼ä»èå®æå¤Itemå¸å±ã
å¨RecylerViewä¸ï¼Adapteræ®æ¼ç两个è§è²ï¼ä¸æ¯æ ¹æ®ä¸åviewTypeå建ä¸ä¹ç¸åºççitemViewï¼äºæ¯è®¿é®æ°æ®éå并å°æ°æ®ç»å®å°æ£ç¡®çViewä¸ãè¿å°±éè¦æ们å®ç°ä»¥ä¸ä¸¤ä¸ªå½æ°ï¼
å¦å¤æ们è¿éè¦éåå¦ä¸ä¸ªæ¹æ³ï¼åListView-Adapteré£æ ·ï¼åæ ·å°åè¯RecyclerView-Adapterå表Itemsçæ»æ°ï¼
ViewHolderæè¿°RecylerViewä¸æ个ä½ç½®çitemViewåå æ°æ®ä¿¡æ¯ï¼å±äºAdapterçä¸é¨åï¼å ¶å®ç°ç±»é常ç¨äºä¿åfindViewByIdçç»æã 主è¦å ç´ ç»ææï¼
å ³äºViewHolderï¼è¿é主è¦ä»ç»mFlagsï¼
FLAG_BOUNDââViewHolderå·²ç»ç»å®å°æ个ä½ç½®ï¼mPositionãmItemIdãmItemViewTypeé½ææ
FLAG_UPDATEââViewHolderç»å®çView对åºçæ°æ®è¿æ¶éè¦éæ°ç»å®ï¼mPositionãmItemIdè¿æ¯ä¸è´ç
FLAG_INVALIDââViewHolderç»å®çView对åºçæ°æ®æ æï¼éè¦å®å ¨éæ°ç»å®ä¸åçæ°æ®
FLAG_REMOVEDââViewHolder对åºçæ°æ®å·²ç»ä»æ°æ®é移é¤
FLAG_NOT_RECYCLABLEââViewHolderä¸è½å¤ç¨
FLAG_RETURNED_FROM_SCRAPââè¿ä¸ªç¶æçViewHolderä¼å å°scrap list被å¤ç¨ã
FLAG_CHANGEDââViewHolderå 容åçååï¼é常ç¨äºè¡¨ææItemAnimatorå¨ç»
FLAG_IGNOREââViewHolderå®å ¨ç±LayoutManager管çï¼ä¸è½å¤ç¨
FLAG_TMP_DETACHEDââViewHolderä»ç¶RecyclerView临æ¶å离çæ å¿ï¼ä¾¿äºåç»ç§»é¤ææ·»å åæ¥
FLAG_ADAPTER_POSITION_UNKNOWNââViewHolderä¸ç¥é对åºçAdapterçä½ç½®ï¼ç´å°ç»å®å°ä¸ä¸ªæ°ä½ç½®
FLAG_ADAPTER_FULLUPDATEââæ¹æ³addChangePayload(null)è°ç¨æ¶è®¾ç½®
LayoutManager主è¦ä½ç¨æ¯ï¼æµéåææ¾RecyclerViewä¸itemViewï¼ä»¥åå½itemView对ç¨æ·ä¸å¯è§æ¶å¾ªç¯å¤ç¨å¤çã
å½æ们æ³å¨æäºitemä¸å ä¸äºç¹æ®çUIæ¶ï¼å¾å¾é½æ¯å¨itemViewä¸å å¸å±å¥½ï¼ç¶åéè¿è®¾ç½®å¯è§æ§æ¥å³å®åªäºä½ç½®æ¾ç¤ºä¸æ¾ç¤ºãRecyclerViewå°itemViewåè£ é¥°UIåéå¼æ¥ï¼è£ 饰UIå³ItemDecorationï¼ä¸»è¦ç¨äºç»å¶itemé´çåå²çº¿ãé«äº®æè marginçãå ¶æºç å¦ä¸ï¼
è¿å»AdapterViewçitem项æä½å¾å¾æ¯æ²¡æå¨ç»çãç°å¨RecyclerViewçItemAnimator使å¾itemçå¨ç»å®ç°åå¾ç®åèæ ·å¼ä¸°å¯ï¼æ们å¯ä»¥èªå®ä¹item项ä¸åæä½ï¼å¦æ·»å ï¼å é¤ï¼çå¨ç»ææã
Recyclerç¨äºç®¡çå·²ç»åºå¼æä¸RecyclerViewå离çï¼scrapped or detachedï¼item viewï¼ä¾¿äºéç¨ãScrapped viewæä¾éäºRecyclerViewï¼ä½è¢«æ 记为å¯ç§»é¤æå¯å¤ç¨çviewã
LayoutManagerè·åAdapteræä¸é¡¹çViewæ¶ä¼ä½¿ç¨Recyclerãå½å¤ç¨çViewææï¼cleanï¼æ¶ï¼Viewè½ç´æ¥è¢«å¤ç¨ï¼åä¹è¥View失æï¼dirtyï¼æ¶ï¼éè¦éæ°ç»å®Viewã对äºææçViewï¼å¦æä¸ä¸»å¨è°ç¨request layoutï¼åä¸éè¦éæ°æµé大å°å°±è½å¤ç¨ãå¨åæRecyclerçå¤ç¨åçä¹åï¼æ们å äºè§£ä¸å¦ä¸ä¸¤ä¸ªç±»ï¼
RecyclerViewPoolç¨äºå¤ä¸ªRecyclerViewä¹é´å ±äº«Viewãåªéè¦å建ä¸ä¸ªRecyclerViewPoolå®ä¾ï¼ç¶åè°ç¨RecyclerViewçsetRecycledViewPool(RecycledViewPool)æ¹æ³å³å¯ãRecyclerViewé»è®¤ä¼å建ä¸ä¸ªRecyclerViewPoolå®ä¾ã
éè¿æºç æ们å¯ä»¥çåºmScrapæ¯ä¸ä¸ª<viewType, List>çæ å°ï¼ mMaxScrap æ¯ä¸ä¸ª<viewType, maxNum>çæ å°ï¼è¿ä¸¤ä¸ªæååé代表å¯å¤ç¨Viewæ± çåºæ¬ä¿¡æ¯ãè°ç¨ setMaxRecycledViews(int viewType, int max) æ¶ï¼å½ç¨äºå¤ç¨ç mScrap ä¸viewType对åºçViewHolder个æ°è¶ è¿maxNumæ¶ï¼ä¼ä»å表æ«å°¾å¼å§ä¸¢å¼è¶ è¿çé¨åãè°ç¨ getRecycledView(int viewType) æ¹æ³æ¶ä» mScrap ä¸ç§»é¤å¹¶è¿åviewType对åºçListçæ«å°¾é¡¹ã
ViewCacheExtensionæ¯ä¸ä¸ªç±å¼åè æ§å¶çå¯ä»¥ä½ä¸ºViewç¼åç帮å©ç±»ãè°ç¨Recycler.getViewForPosition(int)æ¹æ³è·åViewæ¶ï¼Recyclerå æ£æ¥attached scrapåä¸çº§ç¼åï¼å¦æ没æåæ£æ¥ViewCacheExtension.getViewForPositionAndType(Recycler, int, int)ï¼å¦æ没æåæ£æ¥RecyclerViewPoolã注æï¼Recyclerä¸ä¼å¨è¿ä¸ªç±»ä¸åç¼åViewçæä½ï¼æ¯å¦ç¼åViewå®å ¨ç±å¼åè æ§å¶ã
ç°å¨å¤§å®¶çæäºRecyclerViewPoolåViewCacheExtensionçä½ç¨åï¼ä¸é¢å¼å§ä»ç»Recyclerã å¦ä¸æ¯Recyclerçå ä¸ªå ³é®æååéåæ¹æ³ï¼
è·åæ个ä½ç½®éè¦å±ç¤ºçViewï¼å æ£æ¥æ¯å¦æå¯å¤ç¨çViewï¼æ²¡æåå建æ°View并è¿åãå ·ä½è¿ç¨ä¸ºï¼
注ï¼ä»¥ä¸æ¯æ¥å¹é è¿ç¨é½å¯ä»¥å¹é positionæitemIdï¼å¦ææstableIdï¼ã