开发者

How to implement LinearLayout.replaceView() -- or ViewGroup.replaceView() in general

开发者 https://www.devze.com 2023-03-05 15:51 出处:网络
Looking at the documentation, there is LinearLayout.addView() but there isn\'t any LinearLayout.replaceView().

Looking at the documentation, there is LinearLayout.addView() but there isn't any LinearLayout.replaceView().

On the other hand, there is LinearLayout.removeView().

Is implementing my own LinearLayout.replaceView() as two simple successive calls to remove+add safe enough? i.e. are there caveats to开发者_JAVA百科 watch for?

public void replaceView(View oldView, View newView) {
   removeView(oldView);
   addView(newView);
}


Try this:

public void replaceView(final View oldView, final View newView) {
    addView(newView, indexOfChild(oldView));
    removeView(oldView);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消