1.ViewPager2 FragmentStateAdapter 刷新问题
2.AndroidçAdapterçgetVIew()
3.RecyclerView详解
4.layoutinflater.inflate å view.inflate çåºå«
5.è°è°RecyclerViewä¸çç¼å
ViewPager2 FragmentStateAdapter 刷新问题
面临ViewPager2 FragmentStateAdapter刷新难题,经历长时间探索仍未找到解决方法。最终,通过深入源代码研究,数字化校园源码发现了解决之道。
通常情况下,软件源码丢失编写ViewPager2的FragmentStateAdapter时,我们遵循的基本框架如下:
然而,此种方式并未提供直接刷新Fragment的途径。关键点在于,为了实现刷新功能,需要对源代码进行调整,具体操作如下:
1. 重写`getItemCount()`方法。泄露网站源码此方法用于返回集合中Fragment的数量,刷新时需确保该方法能准确反映Fragment的最新状态。
2. 重写`containsItem(itemId: Long)`方法。圈钱网页源码此方法用于检查集合中是否包含特定ID的Fragment,刷新时利用此方法判断和更新特定Fragment。
3. 为每个Fragment分配ID。植入js源码这是实现刷新功能的必要前提,确保能精准定位和操作每个Fragment。
通过以上步骤的实施,成功解决了ViewPager2 FragmentStateAdapter的刷新问题。关键在于深入理解并灵活运用源代码,针对特定需求进行针对性修改,从而达到预期效果。
AndroidçAdapterçgetVIew()
ViewGroup parent å°±æ¯ä½ 设置adapterçé£ä¸ªç»ä»¶éé¢å°è£ ä¸ä¸ªviewGroupç¨æ¥çæ¾item
positionå°±æ¯ä½ éæ©ç itemç第å æ¡ä»0å¼å§
convertViewå°±æ¯itemä¸çå¸å±layoutæè ç»ä»¶
éåAdaperè¦ extends BaseAdapter{
}
ç¶åéåéé¢çæ¹æ³ ç½ä¸å¥½å¤ä¾å ä½æ¯è¦éåçæ¹æ³çåæ°æ¯ä¸è½è¯¥ç ä½ å¯ä»¥å¨ä½ éåçadapterçç±»éé¢ å¢å åéæ¥å®ç°æ°æ®çä¼ é
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ï¼ã
layoutinflater.inflate å view.inflate çåºå«
å¹³æ¶ListViewå è½½itemä¸ï¼adapterçgetViewæ¹æ³ä¸ï¼æ们ç»å¸¸ç¨å°ï¼
LayoutInflater.from(mContext).inflate(R.layout.it,parent,false);
è¿æ ·çæ¹æ³æ¥å è½½å¸å±xml,å¹³æ¶ä¸ç´å°±æ¯è¿ä¹ç¨çï¼ä¹æ²¡ä»ä¹çé®ãä»å¤©ç½ä¸çäºä¸ªèªå®ä¹å¸å±çæºç ï¼èªå®ä¹å¸å±ä¸å è½½å¸å±xmlç¨çView.inflateæ¹æ³ï¼
public class SettingItemView extends RelativeLayout {
private CheckBox cb_status;
private TextView tv_description;
private TextView tv_title;
private String desc_on;
private String desc_off;
private void iniView(Context context) {
View.inflate(context, R.layout.setting_item_view, this);//第ä¸ä¸ªåæ°ä¼ å¸å±æ件çç¶ç±»
cb_status=(CheckBox) this.findViewById(R.id.cb_status);
tv_description=(TextView) this.findViewById(R.id.tv_description);
tv_title=(TextView) this.findViewById(R.id.tv_title);
}
第ä¸æ¬¡è§ç¨è¿ç§æ¹å¼æ¥å è½½å¸å±çï¼çäºä¸ä»çlistviewå è½½item,ä¹æ¯ç¨è¿ç§æ¹å¼ï¼
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if(convertView==null){
view=View.inflate(getApplicationContext(), R.layout.list_item_callsms, null);//æåä¸ä¸ªä¼ äºnull
holder=new ViewHolder();
holder.tv_number=(TextView) view.findViewById(R.id.tv_black_number);
holder.tv_mode=(TextView) view.findViewById(R.id.tv_black_mode);
holder.iv_delete=(ImageView) view.findViewById(R.id.iv_delete);
view.setTag(holder);
好å§ï¼çä¸ä¸View.inflateç说æï¼
Open Declaration View android.view.View.inflate(Context context, int resource, ViewGroup root)
Inflate a view from an XML resource. This convenience method wraps the
LayoutInflater class, which provides a full range of options for view
inflation.
Parameters: context The Context object for your activity or
application. resource The resource ID to inflate root A view group
that will be the parent. Used to properly inflate the layout_
*parameters.
See Also: LayoutInflater
æåæä¸å¥è®©ä½ çLayoutInflaterè¿ä¸ªç±»ï¼æçå®å é¨ä¹æ¯ç¨LayoutInflaterå®ç°çï¼è¿å ¥æºç ï¼
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
æç¶å é¨ä¹æ¯ç¨LayoutInflaterå®ç°çï¼ä¸ç¥é为å¥androidè¿è¦ç¨View.inflatå°è£ ä¸ä¸ããão(ãââ½âã)o
å ¶ä¸LayoutInflaterçInflateçä¸ä¸ªåæ°ææ为ï¼
对äºInflateçä¸ä¸ªåæ°(int resource, ViewGroup root, boolean attachToRoot)
å¦æinflate(layoutId, null )ålayoutIdçæå¤å±çæ§ä»¶ç宽é«æ¯æ²¡æææç
å¦æinflate(layoutId, root, false ) å认为åä¸é¢æææ¯ä¸æ ·ç
å¦æinflate(layoutId, root, true ) å认为è¿æ ·çè¯layoutIdçæå¤å±æ§ä»¶ç宽é«æè½æ£å¸¸æ¾ç¤º
对è¿ä¸ä¸ªåæ°åºå«ä¸ç解çè¯å¯ä»¥çè¿ç¯æç« ï¼
inflate第ä¸ä¸ªåæ°ææ
ä»æºç è§åº¦è§£æçæé大ç¥çï¼
Android LayoutInflateråçåæï¼å¸¦ä½ ä¸æ¥æ¥æ·±å ¥äºè§£View(ä¸)
以åå¦ä¸ç¯æè§å¾ä¸éçï¼
Android LayoutInflate深度解æ ç»ä½ 带æ¥å ¨æ°ç认è¯
çå®ï¼ä½ åºè¯¥ç¥éè¿ä¸ªåæ°ææäºï¼okï¼åæ¥çä¸é¢ä»£ç ï¼ è¿æ¶å°±å¯ä»¥æ¿æ¢ä¸ºlayoutInflaterçæ¹å¼äºï¼
对äºç¬¬ä¸ä¸ªèªå®ä¹å¸å±ï¼
//View.inflate(context, R.layout.setting_item_view, this);//第ä¸ä¸ªåæ°ä¼ å¸å±æ件çç¶ç±»
LayoutInflater.from(context).inflate(R.layout.setting_item_view, this, true);//çä»·äºä¸é¢
第äºä¸ªéé å¨ä¸getViewï¼
//view=View.inflate(getApplicationContext(), R.layout.list_item_callsms, null);
view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item_callsms,parent,false);
è°è°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()æ¹æ³ã