【要饭施舍源码】【江苏距离广西源码】【soring 动态代理源码】layoutmanager源码解析
1.è°è°RecyclerViewä¸çç¼å
2.MyAndroidRecyclerView实现头部悬浮吸顶效果且可点击--100个经典UI设计模板(96/100)
3.RecyclerView详解
è°è°RecyclerViewä¸çç¼å
Androidæ·±å ¥ç解RecyclerViewçç¼åæºå¶
RecyclerViewå¨é¡¹ç®ä¸ç使ç¨å·²ç»å¾æ®éäºï¼å¯ä»¥è¯´æ¯é¡¹ç®ä¸æé«é¢ä½¿ç¨çä¸ä¸ªæ§ä»¶äºãé¤äºå¸å±çµæ´»æ§ã丰å¯çå¨ç»ï¼RecyclerViewè¿æä¼ç§çç¼åæºå¶ï¼æ¬æå°è¯éè¿æºç æ·±å ¥äºè§£ä¸ä¸RecyclerViewä¸çç¼åæºå¶ã
RecyclerViewåæ§è½ä¼åè¦è¯´å¤æä¹å¤æï¼æ¯å¦è¯´å¸å±ä¼åï¼ç¼åï¼é¢å è½½ççãå ¶ä¼åçç¹å¾å¤ï¼å¨è¿äºçä¼¼ç¬ç«çç¹ä¹é´ï¼å ¶å®åå¨ä¸ä¸ªæ¢çº½ï¼Adapterãå 为ææçViewHolderçå建åå 容çç»å®é½éè¦ç»è¿Adaperç两个å½æ°onCreateViewHolderåonBindViewHolderã
å æ¤æ们æ§è½ä¼åçæ¬è´¨å°±æ¯è¦åå°è¿ä¸¤ä¸ªå½æ°çè°ç¨æ¶é´åè°ç¨ç次æ°ãå¦ææ们æ³å¯¹RecyclerViewåæ§è½ä¼åï¼å¿ é¡»æ¸ æ¥çäºè§£å°æ们çæ¯ä¸æ¥æä½èåï¼onCreateViewHolderåonBindViewHolderè°ç¨äºå¤å°æ¬¡ãå æ¤ï¼äºè§£RecyclerViewçç¼åæºå¶æ¯RecyclerViewæ§è½ä¼åçåºç¡ã
为äºç解ç¼åçåºç¨åºæ¯ï¼æ¬æé¦å ä¼ç®åä»ç»ä¸ä¸RecyclerViewçç»å¶åçï¼ç¶åååæå ¶ç¼åå®ç°åçã
RecyclerViewæ»å¨æ¶ä¼è§¦åonTouchEvent#onMoveï¼åæ¶åå¤ç¨ViewHolderå¨è¿éå°±ä¼å¼å§ãæ们ç¥é设置RecyclerViewæ¶éè¦è®¾ç½®LayoutManager,码解LayoutManagerè´è´£RecyclerViewçå¸å±ï¼å å«å¯¹ItemViewçè·åä¸å¤ç¨ã以LinearLayoutManager为ä¾ï¼å½RecyclerViewéæ°å¸å±æ¶ä¼ä¾æ¬¡æ§è¡ä¸é¢å 个æ¹æ³ï¼
ä¸è¿°çæ´ä¸ªè°ç¨é¾ï¼onLayoutChildren()->fill()->layoutChunk()->next()->getViewForPosition(),getViewForPosition()å³æ¯æ¯ä»RecyclerViewçåæ¶æºå¶å®ç°ç±»Recyclerä¸è·ååéçViewï¼ä¸é¢ä¸»è¦å°±æ¥ä»çè¿ä¸ªRecycler#getViewForPosition()çå®ç°ã
ä¸è¿°é»è¾ç¨æµç¨å¾è¡¨ç¤ºï¼
RecyclerViewå¨Recyleréé¢å®ç°ViewHolderçç¼åï¼Recycleréé¢çå®ç°ç¼åç主è¦å å«ä»¥ä¸5个对象ï¼
public final class Recycler {
final ArrayList mAttachedScrap = new ArrayList<>();
ArrayList mChangedScrap = null;
RecyclerViewå¨è®¾è®¡çæ¶å讲ä¸è¿°5个ç¼å对象å为äº3级ãæ¯æ¬¡å建ViewHolderçæ¶åï¼ä¼æç §ä¼å 级ä¾æ¬¡æ¥è¯¢ç¼åå建ViewHolderãæ¯æ¬¡è®²ViewHolderç¼åå°Recyclerç¼åçæ¶åï¼ä¹ä¼æç §ä¼å 级ä¾æ¬¡ç¼åè¿å»ãä¸çº§ç¼ååå«æ¯ï¼
使ç¨èªå®ä¹ViewCacheExtensionåï¼view离å±åååæ¥ä¸ä¼èµ°onBindViewHolder()æ¹æ³ã
holder.setIsRecyclable(false)ï¼è¿æ ·çè¯æ¯æ¬¡é½ä¼èµ°onCreateViewHolder()åonBindViewHolder()æ¹æ³
1.æååå§åviewHolderï¼æ¾å°ç¼åæ± ä¸
viewPool.putRecycledView(adapter.onCreateViewHolder(recyclerView, 1))
2.æååå§åviewï¼å¨onCreateViewHolderçæ¶åå»åview
3.èªå®ä¹ViewCacheExtension
4.éå½çå¢å cacheSize
4.å ¬ç¨ç¼åæ± ï¼æ¯å¦å¤ä¸ªviewPager+fragmentåºæ¯ä½¿ç¨ï¼æè å ¨å±åå©ç¼åæ± ï¼æè§ç¨æ·ä¸å¤§ã
æ2ä¸åæ³æå¼
第ä¸ç§
第äºç§
ä¸ä¼ï¼å 为prefetchï¼GapWorkerä¸çä¸ä¸ªæ¹æ³ï¼ä¹åmViewCacheMaxä¼åæmRequestedCacheMax + extraCache
æ2ç§æ¹å¼å¯ä»¥è®©ç¼å失æ
第ä¸ç§
recyclerView.setItemViewCacheSize(-1)
第äºç§
recyclerView.setItemViewCacheSize(0)
layoutManager.isItemPrefetchEnabled = false
设置ä¸ç¼ååï¼æ¥åæ»å¨è®©viewè¿å ¥å±å¹ç¦»å¼å±å¹ï¼viewHolderçitemæ¶ä¼å¤æ¬¡èµ°onBindViewHolder()æ¹æ³ã
MyAndroidRecyclerView实现头部悬浮吸顶效果且可点击--个经典UI设计模板(/)
本文将展示RecyclerView如何实现头部悬浮吸顶效果,并提供个经典UI设计模板中的码解一种实例。通过自定义StickyHeadContainer、码解OnStickyChangeListener和StickyItemDecoration,码解要饭施舍源码以及适配器的码解设置,我们可以实现头部的码解江苏距离广西源码可滚动和点击功能。
首先,码解代码结构主要包括RecyclerView、码解LinearLayoutManager和Adapter。码解在布局中,码解关键部分是码解StickyHeadContainer的使用,如在StockActivity的码解initView()方法中。要确保头部悬浮吸顶效果的码解soring 动态代理源码显示,务必在RecyclerViewAdapter中设置mDataCallback。码解StickyHeadContainer的码解onDataChange()方法是实现吸顶效果的关键,其源码清晰易懂,源码更换图片可以从gitee.com/lc/my-andr...获取。
此外,本文还提到了Mcflag/Expan...项目作为参考,源码分析与运营提供了粘性头部的点击功能。如果你对个经典UI设计模板中的其他部分感兴趣,smartApi是一个好用的API接口开发工具。smartApi-v1.0.0版本在年9月日上线,它是一款简化版的postman,具有基础功能。关于smartApi的下载地址,可以访问pan.baidu.com/s/1m5dY_p...
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ï¼ã