使用RecyclerView過(guò)程中遇到異常:
創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)山亭,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):13518219792
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
繼承并重寫(xiě)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)用時(shí)使用WrappedLinearLayoutManager代替LinearLayoutManager
val recyclerAdapter = RecyclerViewAdapter(activity) val manager = WrapContentLinearLayoutManager(context).apply { orientation = LinearLayoutManager.VERTICAL } val recyclerView = view.findViewById(R.id.recycler_view).apply { layoutManager = manager adapter = recyclerAdapter }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。