Android examples for User Interface:View
unbind View References
//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 { public static void unbindReferences(View view) { try {//from w ww .j av a2 s . c o m if (view != null) { unbindViewReferences(view); if (view instanceof ViewGroup) { unbindViewGroupReferences((ViewGroup) view); } } } catch (Throwable e) { } } @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(); } } 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); } if (!(viewGroup instanceof AdapterView)) { viewGroup.removeAllViews(); } } } }