[Android][筆記]Replace View

  • 859
  • 0

摘要:[Android][筆記]Replace View

最近剛好有做到Andorid的動態面版,要在Fragement裡用GridLayout放入多個Customization的View,

不知道怎麼的剛好要用到Replace View這個功能而不重新CreateView,解法如下:

 

public static ViewGroup Help_getParent(View view) {
      return (ViewGroup) view.getParent();
}
 
public static void Help_removeView(View view) {
      ViewGroup parent = Help_getParent(view);
      if (parent != null) {
            parent.removeView(view);
      }
}
 
public static void Help_replaceView(View currentView, View newView) {
      ViewGroup parent = Help_getParent(currentView);
      if (parent == null) {
          return;
      }
     final int index = parent.indexOfChild(currentView);
     Help_removeView(currentView);
     Help_removeView(newView);
     parent.addView(newView, index);
}