之前app同事遇到一個問題,需要修改viewgroup中view的節點位置。
給提供了一個方法,其實很多時候,這種方法在源碼中都能找到的。
view樹中,都是有節點的,有相應的序號。
可能特定需求下,需要動態變更,如何更改呢?
private void bringToIndex(View child, int index) {
ViewGroup mParent = (ViewGroup) child.getParent();
if (index >= 0) {
mParent.removeView(child);
mParent.addView(child, index);
mParent.requestLayout();
mParent.invalidate();
}
}
調用方法:bringToIndex(child, index);
bringToIndex(view1, 1);
再換回來:bringToIndex(view2, 1);