Answers:
viewGroup.removeAllViews()
适用于任何viewGroup。在您的情况下是GridView。
http://developer.android.com/reference/android/view/ViewGroup.html#removeAllViews()
ViewGroup
?
您可以使用以下功能在ViewGroup中仅删除某些类型的视图:
private void clearImageView(ViewGroup v) {
boolean doBreak = false;
while (!doBreak) {
int childCount = v.getChildCount();
int i;
for(i=0; i<childCount; i++) {
View currentChild = v.getChildAt(i);
// Change ImageView with your desired type view
if (currentChild instanceof ImageView) {
v.removeView(currentChild);
break;
}
}
if (i == childCount) {
doBreak = true;
}
}
}
试试这个
void removeAllChildViews(ViewGroup viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
if (child instanceof AdapterView) {
viewGroup.removeView(child);
return;
}
removeAllChildViews(((ViewGroup) child));
} else {
viewGroup.removeView(child);
}
}
}