Fragment with RecyclerView : java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true

后端 未结 2 1245
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 07:33

I keep getting java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true when using Fragment with Re

2条回答
  •  走了就别回头了
    2021-01-19 08:16

    I saw this happen for me when I used a custom object in the ViewHolder for the Recycler View adapter.

    To fix the issue I cleared the custom object which in my case was a timer in the onViewRecycled(ViewHolder holder) for the adapter as below:

    public void onViewRecycled(ViewHolder holder) {
        if(holder instanceof  EntityViewHolder) {
            if(((EntityViewHolder)holder).timer != null) {
                ((EntityViewHolder) holder).timer.cancel();
            }
        }
        super.onViewRecycled(holder);
    }
    

    This fixed the bug.

提交回复
热议问题