RecyclerView crashes when “scrapped or attached views may not be recycled”

前端 未结 27 2872
灰色年华
灰色年华 2020-11-29 19:50

I\'m using a simple implementation of RecyclerView taken from the Android website using a StaggeredGridLayoutManager and I keep getting this error

27条回答
  •  野性不改
    2020-11-29 20:12

    I saw this happen for me when I used a custom object in the ViewHolder for the RecyclerView 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.

提交回复
热议问题