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

前端 未结 27 2897
灰色年华
灰色年华 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:10

    Workaround solution if reason of Exception is what itemView has parent. In code, where you have notifyItemRemoved(position), remove itemView from RecyclerView:

    View itemView = mRecyclerView.getLayoutManager().findViewByPosition(position);
    if (itemView != null && itemView.getParent() != null) {
        ((ViewGroup) itemView.getParent()).removeView(itemView);
    }
    notifyItemRemoved(position);
    

提交回复
热议问题