Android - Removing recursive representations in layout

Is there a way to recursively remove views from the layout? The removeAllViewsInLayout () function does not seem to complete the task (it removes all views from the layout, but does not display the layouts views). In my program, I add several FrameLayouts dynamically over RelativeLayout, and over these FrameLayouts I add some ImageViews and TextViews. So I want to know if I need to do my own recursive delete-view code or if there are some available.

+6
source share
3 answers

I think you can view the layout as a tree. So you only need a reference to the root of the node. If you delete it, it will be assembled along with its children, since nothing refers to them.

If for some reason you still see some views after this operation, it means that they belong to another node. You can always use the hierarchy viewer to “see” your interface better: http://developer.android.com/guide/developing/debugging/debugging-ui.html

+3
source

This is not a recursive example, but I believe that it can solve your problem:

public void ClearRelLayout(RelativeLayout RL){ for(int x=0;x<RL.getChildCount();x++){ if(RL.getChildAt(x) instanceof FrameLayout){ FrameLayout FL = (FrameLayout) RL.getChildAt(x); FL.removeAllViewsInLayout(); } } RL.removeAllViewsInLayout(); } 

Instead of using recursion, simply swipe through each child and clear it if it's FrameLayout

0
source

First, usually you should call removeAllViews() instead of removeAllViewsInLayout() to read the JavaDoc for the difference.

In addition, none of these functions is recursive; they only remove representations from the object on which it is called. Substructures will not change. This can cause problems and even memory leaks if they have references to other objects that are stored.

For example, if you have the following structure:

  <LinearLayout id='top'> <LinearLayout id='sub'> <Button id='b'/> </LinearLayout> </LinearLayout> 

and call removeAllViews() on top sub layout will still refer to b (and b will refer to sub as the parent). This means that you cannot reuse b anywhere else without first deleting its parent ( sub ). If you do, you will get an exception.

0
source

Source: https://habr.com/ru/post/894727/


All Articles