我有一个布局,其中有一个recyclerView
,其中包括
两个选项卡(CustomView),根据选项卡的选择,我想更新这些选项卡下的UI。但是正如标题所述,当我尝试notifyData时,应用程序崩溃了。
我检查了类似的帖子,并尝试了以下建议:
A)使用runOnUiThread
呼叫notifyDataSetChange
B)要使用Handler
,然后再次调用notifyDataSetChange()
此外,我尝试使用notifyItemChanged(position)
,但应用再次崩溃。
以下代码显示了选项卡的接口方法。我也尝试在activity
上实现它,但应用程序再次崩溃。使用以下代码,当我尝试第二次更改选定的选项卡时,应用程序崩溃。
回收站适配器代码:
override fun onSelectedTab(selectedTab: String, selectedTransaction: String) {
presetPrepayMainModelList[adapterPosition].transactionTypeHashMap = mapOf(TRANSACTION_TYPE.ordinal to selectedTab)
if (selectedTransaction == "preset") || selectedTransaction == "prepay") {
presetPrepaySelectedValues.selectedTransactionType = selectedTransaction
onPresetPrepayMainRecyclerViewListener.onTransactionTypeSelected(selectedTransaction, presetPrepayMainModelList)
notifyItemRangeChanged(adapterPosition + 1, presetPrepayMainModelList.size)
} else {
presetPrepaySelectedValues.selectedTransactionLimit = selectedTransaction
notifyItemRangeChanged(adapterPosition + 2, presetPrepayMainModelList.size)
}
}
错误是:
Process: , PID: 19535
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView{f026357 VFED..... .F....ID 0,399-1080,1515 #7f0a00ec app:id/presetPrepayMainRecyclerView}, adapter:.modules.main_flow.fragments.pumps.activities.pump.activities.preset_prepay.adapter.PresetPrepayMainRecyclerViewAdapter@a92fe44, layout:android.support.v7.widget.LinearLayoutManager@6a72b2d, context:.modules.main_flow.fragments.pumps.activities.pump.activities.preset_prepay.PresetPrepayActivity@85bcfe2
at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2880)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onItemRangeChanged(RecyclerView.java:5292)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(RecyclerView.java:12012)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(RecyclerView.java:12002)
at android.support.v7.widget.RecyclerView$Adapter.notifyItemRangeChanged(RecyclerView.java:7132)
at .modules.main_flow.fragments.pumps.activities.pump.activities.preset_prepay.adapter.PresetPrepayMainRecyclerViewAdapter$SelectTransactionTypeViewHolder.onSelectedTab(PresetPrepayMainRecyclerViewAdapter.kt:124)
at .custom_view.TwoOptionsTabCustomView.selectTab(TwoOptionsTabCustomView.kt:109)
at .custom_view.TwoOptionsTabCustomView.initTwoOptionsTabCustomViewWithImageOnTheTopSide(TwoOptionsTabCustomView.kt:82)
at .modules.main_flow.fragments.pumps.activities.pump.activities.preset_prepay.adapter.PresetPrepayMainRecyclerViewAdapter$SelectTransactionTypeViewHolder.setTheValuesToPresetPrepayTabs(PresetPrepayMainRecyclerViewAdapter.kt:108)
at .modules.main_flow.fragments.pumps.activities.pump.activities.preset_prepay.adapter.PresetPrepayMainRecyclerViewAdapter$SelectTransactionTypeViewHolder.bindData(PresetPrepayMainRecyclerViewAdapter.kt:81)
at .modules.main_flow.fragments.pumps.activities.pump.activities.preset_prepay.adapter.PresetPrepayMainRecyclerViewAdapter.onBindViewHolder(PresetPrepayMainRecyclerViewAdapter.kt:58)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
at android.view.View.measure(View.java:20151)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at and``
答案 0 :(得分:0)
伙计们,我找到了解决我问题的方法。所以,如果将来有人需要,我会在这里发布。解决方案是使用异步任务并对列表进行更改,然后通知适配器!