RecyclerView gets crashed [IllegalArgumentException] when use notifyItemChanged from Handler using Runnable

99封情书 提交于 2019-12-03 01:19:05

We also had this issue on our application, and after a really long debugging session we found out that it was caused by adapter.setHasStableIds(true)

We removed the offending line and the issue is finally gone.

Hope it helps.

i had the same issue,i could handle it by disabling the animator of recyclerview:

recyclerView.itemAnimator = null

I faced the same problem but turned out the reason was calling scrollToPosition. The solution that worked for me after many attempts was to call notifyDataSetChanged before scrolling (all from the UI thread of course). Maybe it will help someone.

adapter.notifyItemChanged(position);must be called from the main thread

instead of your Handler use the Handler with mainLooper

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            //HERE
        }
    });

There are several workarounds to the problem:

  1. Call to notifyDataSetChanged() instead notifyItemChanged(). It is the less efficient way to solve it.

  2. As pointed by Henrique de Sousa, remove the line adapter.setHasStableIds(true) prevents the problem.

But the real solution (as commented by southerton), if the adapter is stable, is to implement and correctly getItemId(), honouring the "hasStableIds" value set.

Since this is the first post to come up I'll post another solution to the problem which helped in my specific case: I was hiding the parentview containing the recyclerview using

parentView.setVisibility(View.GONE);

recyclerview does not like that, apparently if you want to hide a view which contains a recyclerview, you can only use View.INVISIBLE.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!