unbind View Group References - Android User Interface

Android examples for User Interface:View

Description

unbind View Group References

Demo Code


//package com.java2s;

import android.graphics.drawable.Drawable;
import android.os.Build;

import android.view.View;
import android.view.ViewGroup;

import android.webkit.WebView;
import android.widget.AdapterView;

import android.widget.ImageView;
import android.widget.Spinner;

public class Main {
    private static void unbindViewGroupReferences(ViewGroup viewGroup) {
        if (viewGroup instanceof ViewGroup) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                unbindViewReferences(child);
            }/*from   ww w .  jav  a  2s . c  om*/
            if (!(viewGroup instanceof AdapterView)) {
                viewGroup.removeAllViews();
            }
        }
    }

    @SuppressWarnings({ "deprecation", "rawtypes" })
    private static void unbindViewReferences(View view) {
        try {
            if (view instanceof AdapterView && !(view instanceof Spinner)) {
                ((AdapterView) view).setOnItemClickListener(null);
            } else {
                view.setOnClickListener(null);
            }
            view.setOnCreateContextMenuListener(null);
            view.setOnFocusChangeListener(null);
            view.setOnKeyListener(null);
            view.setOnLongClickListener(null);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        // set background to null
        Drawable d = view.getBackground();
        if (d != null) {
            d.setCallback(null);
        }

        if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            d = imageView.getDrawable();
            if (d != null) {
                d.setCallback(null);
            }

            imageView.setImageDrawable(null);
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                imageView.setBackgroundDrawable(null);
            } else {
                imageView.setBackground(null);
            }
        }

        // destroy hybrid_webview_for_dsmpages
        else if (view instanceof WebView) {
            ((WebView) view).stopLoading();
            ((WebView) view).clearCache(true);
            ((WebView) view).destroyDrawingCache();
            ((WebView) view).setWebChromeClient(null);
            ((WebView) view).setWebViewClient(null);
            ((WebView) view).removeAllViews();
            ((WebView) view).destroy();
        }
    }
}

Related Tutorials