从视图中删除所有子视图

我将如何从小部件中删除所有的子视图? 例如,我有一个GridView,我dynamic膨胀许多其他的LinearLayout到它; 后来在我的应用程序中,我正在寻找新的GridView并清除所有的子视图。 我将如何做到这一点? TIA。

viewGroup.removeAllViews() 

适用于任何viewGroup。 在你的情况下,它是GridView。

http://developer.android.com/reference/android/view/ViewGroup.html#removeAllViews();

你可以使用这个函数只删除ViewGroup中的某些types的视图:

 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); } } }