RecyclerView索引溢出異常的解決方法
使用RecyclerView過程中遇到異常:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
繼承并重寫LinearLayoutManager.onLayoutChildren()方法
class WrappedLinearLayoutManager : LinearLayoutManager {
constructor(context: Context) : super(context)
constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State) {
try {
super.onLayoutChildren(recycler, state)
} catch (e: IndexOutOfBoundsException) {
e.printStackTrace()
}
}
}
調(diào)用時使用WrappedLinearLayoutManager代替LinearLayoutManager
val recyclerAdapter = RecyclerViewAdapter(activity)
val manager = WrapContentLinearLayoutManager(context).apply { orientation = LinearLayoutManager.VERTICAL }
val recyclerView = view.findViewById<RecyclerView>(R.id.recycler_view).apply {
layoutManager = manager
adapter = recyclerAdapter
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- RecyclerView實現(xiàn)仿支付寶應(yīng)用管理
- RecyclerView焦點跳轉(zhuǎn)BUG優(yōu)化的方法
- Android RecyclerView的焦點記憶封裝
- Android 基于RecyclerView實現(xiàn)的歌詞滾動自定義控件
- Android RecyclerView 滾動到中間位置的方法示例
- 淺談Android RecyclerView UI的滾動控件示例
- 詳解如何在Android Studio中添加RecyclerView-v7支持包
- Android RecyclerView添加搜索過濾器的示例代碼
- RecyclerView實現(xiàn)查看更多及收起
相關(guān)文章
Android App中實現(xiàn)相冊瀑布流展示的實例分享
這篇文章主要介紹了Android App中實現(xiàn)相冊瀑布流展示的實例分享,例子中利用到了緩存LruCache類的相關(guān)算法來解決大量加載問題,需要的朋友可以參考下2016-04-04
Android Studio 配置忽略文件的方法實現(xiàn)
這篇文章主要介紹了Android Studio 配置忽略文件的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
學(xué)習(xí)Android自定義Spinner適配器
這篇文章主要為大家詳細介紹了學(xué)習(xí)Android自定義Spinner適配器的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
Android自定義ViewGroup實現(xiàn)右滑進入詳情
這篇文章主要為大家詳細介紹了Android如何通過自定義ViewGroup實現(xiàn)右滑進入詳情效果,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-01-01
android dialog邊框去除白色邊框?qū)崿F(xiàn)思路及代碼
android dialog邊框含有白色真是美中不足啊,本文將介紹如何去除白色邊框,有思路及代碼,感興趣的朋友可以了解下2013-01-01
Android WebView調(diào)用本地相冊的方法
這篇文章主要為大家詳細介紹了Android WebView調(diào)用本地相冊的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12

