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

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

    I solve this problem by removing parent.addView() in onCreateViewHolder

    This is my code

    public MyViewHolder onCreateViewwHolder(ViewGroup parent, int viewType)  {
        Button addButton = new Button(context);
        //parent.addView(addButton);
        return new MyViewHolder(addButton);
    }
    

    Function at android.support.v7.widget.RecyclerViewRecycler.recyclerViewHolderinternal() check whether my button has already a parent or not. Which if we add button to parent, it will be also assign RecyclerView to its mParent variable.

提交回复
热议问题