修复RecyclerView异常:java.lang.IndexOutOfBoundsException: Inconsistency detected
This commit is contained in:
xuxinchao
2022-06-07 16:47:40 +08:00
parent 9659d81018
commit bba4b34ebf
3 changed files with 38 additions and 2 deletions

View File

@@ -55,7 +55,7 @@ class IPCReportWindow constructor(activity: Activity) : View.OnTouchListener{
it.alpha = 0.9f
}
ipcReportAdapter = IPCReportAdapter()
rvIPCReport.layoutManager = LinearLayoutManager(mActivity,
rvIPCReport.layoutManager = WrapContentLinearLayoutManager(mActivity,
LinearLayoutManager.VERTICAL,false)
rvIPCReport.adapter = ipcReportAdapter

View File

@@ -50,7 +50,7 @@ class ReportListFloatWindow constructor(activity: Activity) : View.OnTouchListen
it.alpha = 0.9f
}
reportListAdapter= ReportListAdapter(mActivity)
rvReportList.layoutManager = LinearLayoutManager(mActivity,
rvReportList.layoutManager = WrapContentLinearLayoutManager(mActivity,
LinearLayoutManager.VERTICAL,false)
rvReportList.adapter = reportListAdapter
}

View File

@@ -0,0 +1,36 @@
package com.mogo.eagle.core.function.hmi.ui.setting
import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
/**
* @author XuXinChao
* @description fix java.lang.IndexOutOfBoundsException检测到不一致。视图持有者适配器positionViewHolder无效
* @since: 2022/6/7
*/
class WrapContentLinearLayoutManager : 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()
}
}
}