在使用RecyclerView的时候,报了一个错误
The specified child already has a parent. You must call removeView() on the child's parent first.
从问题看,是重复添加了parent,经查,原来问题在这里:
@Override
public RemindBACItemAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_remind_bac, parent);
return new ItemViewHolder(view);
}
一个View只能有一个父控件。当第二次添加的时候,要改变这个控件的父控件(虽然是同一个父控件,但是也要通过改变该View的父控件来实现)。
运行时又不允许一个有父控件的子控件,再去改变他的父控件。
这里的item_remind_bac是一个LinearLayout, 其parent就是这个LinearLayout.
代码里将RecyclerView再作为这个item_remind_bac的parent的话,就会报错.
改为如下即可:
View view = LayoutInflater.from(mContext).inflate(R.layout.item_remind_bac, parent, false);
网友评论