Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Remove views in a layout recursively

Tags:

android

Is there a way to remove the views from a layout recursively? The function removeAllViewsInLayout() does not seem to do the task (it removes all views from a layout, but not the layout's children views). In my program, I add some FrameLayouts dynamically over a RelativeLayout, and over these FrameLayouts I add some ImageViews and TextViews. So I want to know if I'll have to go make my own recursive removing-view-code or if there is some available.

like image 399
Alesqui Avatar asked Aug 10 '11 14:08

Alesqui


2 Answers

I think you can consider the layout as a tree. Thus, you only need a reference to the root node. If you remove it, it will be collected along with its children, since nothing else references them.

If for some reason you still see some Views after this operation, it means they belonged to a different node. You can always use the Hierarchy Viewer to "see" your UI better: http://developer.android.com/guide/developing/debugging/debugging-ui.html

like image 82
Erdal Avatar answered Oct 10 '22 07:10

Erdal


It isn't 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();
}

Rather than using recursion, just loop through each child and clear it if it is a FrameLayout

like image 34
MrZander Avatar answered Oct 10 '22 09:10

MrZander