Android RecyclerView顯示Item布局不一致解決辦法
RecyclerView顯示Item布局不一致
在自定義RecyclerAdapter的時(shí)候,在重寫(xiě)onCreateViewHolder方法是使用了
@Override public H onCreateViewHolder(ViewGroup parent, int viewType) { View view=View.inflate(context,layoutId,null); return view; }
進(jìn)行生成布局,結(jié)果發(fā)現(xiàn)生成的布局沒(méi)有LayoutParams。以前自定義View的時(shí)候發(fā)現(xiàn),LayoutParams是由于ViewGroup生成的,因?yàn)檫@里添加的ViewGroup為null。所以并不會(huì)生成LayoutParams。結(jié)果在RecyclerView的getViewForPosition方法中檢查了有沒(méi)有LayoutParams如果沒(méi)有的話就調(diào)用LayoutManager的generateDefaultLayoutParams生成默認(rèn)的LayoutParames。代碼段如下:
final ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); final LayoutParams rvLayoutParams; if (lp == null) { rvLayoutParams = (LayoutParams) generateDefaultLayoutParams(); holder.itemView.setLayoutParams(rvLayoutParams); } else if (!checkLayoutParams(lp)) { rvLayoutParams = (LayoutParams) generateLayoutParams(lp); holder.itemView.setLayoutParams(rvLayoutParams); } else { rvLayoutParams = (LayoutParams) lp; }
而在LinearLayoutManager中g(shù)enerateDefaultLayoutParams方法實(shí)現(xiàn)如下。
/** * {@inheritDoc} */ @Override public LayoutParams generateDefaultLayoutParams() { return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); }
最終會(huì)造成RecycleView的顯示效果與布局文件不一致。后來(lái)使用了LayoutInflater來(lái)填充布局。
@Override public H onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(layoutId, parent, false); return getInstanceOfH(view); }
查看LayoutInflater源碼發(fā)現(xiàn)inflate最后的參數(shù)如果是false的話就不會(huì)將生成的View添加到parent。但是會(huì)根據(jù)parent產(chǎn)生相應(yīng)的LayoutParams 。源碼如下:
* @param attachToRoot Whether the inflated hierarchy should be attached to * the root parameter? If false, root is only used to create the * correct subclass of LayoutParams for the root view in the XML.
因?yàn)樵趏nCreateViewHolder中產(chǎn)生的View不能由我們手動(dòng)添加到RecycleView中所以最后的參數(shù)只能是false;
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android RecyclerView滑動(dòng)刪除和拖動(dòng)排序
- Android RecyclerView item選中放大被遮擋問(wèn)題詳解
- Android RecyclerView實(shí)現(xiàn)多種item布局的方法
- Android使用CardView作為RecyclerView的Item并實(shí)現(xiàn)拖拽和左滑刪除
- Android中RecyclerView實(shí)現(xiàn)Item添加和刪除的代碼示例
- Android中RecyclerView的item寬高問(wèn)題詳解
- Android RecyclerView的Item點(diǎn)擊事件實(shí)現(xiàn)整理
- Android 中RecyclerView多種item布局的寫(xiě)法(頭布局+腳布局)
- Android RecyclerView自由拖動(dòng)item的實(shí)現(xiàn)代碼
相關(guān)文章
Android Toolbar應(yīng)用欄使用方法簡(jiǎn)介
App中應(yīng)用欄是十分常見(jiàn)的,通常應(yīng)用欄會(huì)顯示當(dāng)前頁(yè)面的標(biāo)題,還有一些操作按鈕,例如返回、搜索、掃碼等。本文介紹如何通過(guò)Toolbar實(shí)現(xiàn)應(yīng)用欄2022-12-12Android Studio 報(bào)錯(cuò)“app:processDebugResources"解決方法
這篇文章主要介紹了Android Studio 報(bào)錯(cuò)“app:processDebugResources"解決方法的相關(guān)資料,需要的朋友可以參考下2017-07-07Android編程判斷是否連接網(wǎng)絡(luò)的方法【W(wǎng)iFi及3G判斷】
這篇文章主要介紹了Android編程判斷是否連接網(wǎng)絡(luò)的方法,結(jié)合實(shí)例形式分析了Android針對(duì)WiFi及3G網(wǎng)絡(luò)連接的判斷方法,需要的朋友可以參考下2017-02-02Android Studio 配置:自定義頭部代碼注釋及添加模版方式
這篇文章主要介紹了Android Studio 配置:自定義頭部代碼注釋及添加模版方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03學(xué)習(xí)使用Material Design控件(四)Android實(shí)現(xiàn)標(biāo)題欄自動(dòng)縮放、放大效果
這篇文章主要為大家介紹了學(xué)習(xí)使用Material Design控件的詳細(xì)教程,Android實(shí)現(xiàn)標(biāo)題欄自動(dòng)縮放、放大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android計(jì)步功能的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android計(jì)步功能的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03用Android Location獲取當(dāng)前地理位置的方法
本篇文章小編為大家介紹,用Android Location獲取當(dāng)前地理位置的方法。需要的朋友參考下2013-04-04Android 中使用EditText 點(diǎn)擊全選再次點(diǎn)擊取消全選功能
這篇文章主要介紹了Android 中使用EditText 點(diǎn)擊全選再次點(diǎn)擊取消全選功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12