android系统中已经提供了一些删除父控件中的方法。
如:.removeAllViews();
.removeViews(start, count);
.removeViewsInLayout(start, count);
....
但是有时这些并不是太满足需求,自己写删除子view的方法:
View view = null;(mContentLayout是父控件)
for(int index = mContentLayout.getChildCount();index > 0;index --){
view = mContentLayout.getChildAt(index);
if(view != null && view instanceof RecyclerView){
} else {
mContentLayout.removeView(view);
}
}
注意:如果索引从index = 0开始删除的话,方法执行完之后,子view还存在,不能删除。(参考removeAllViews系统源码)