Example usage for android.widget ListPopupWindow setOnDismissListener

List of usage examples for android.widget ListPopupWindow setOnDismissListener

Introduction

In this page you can find the example usage for android.widget ListPopupWindow setOnDismissListener.

Prototype

public void setOnDismissListener(@Nullable PopupWindow.OnDismissListener listener) 

Source Link

Document

Set a listener to receive a callback when the popup is dismissed.

Usage

From source file:com.brandroidtools.filemanager.activities.NavigationActivity.java

/**
 * Method that shows a popup with a menu associated a {@link com.brandroidtools.filemanager.preferences.FileManagerSettings}.
 *
 * @param anchor The action button that was pressed
 * @param settings The array of settings associated with the action button
 *//* w w  w . j  ava 2  s . c o m*/
private void showSettingsPopUp(View anchor, List<FileManagerSettings> settings) {
    //Create the adapter
    final MenuSettingsAdapter adapter = new MenuSettingsAdapter(this, settings);

    //Create a show the popup menu
    final ListPopupWindow popup = DialogHelper.createListPopupWindow(this, adapter, anchor);
    popup.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            FileManagerSettings setting = ((MenuSettingsAdapter) parent.getAdapter()).getSetting(position);
            final int value = ((MenuSettingsAdapter) parent.getAdapter()).getId(position);
            popup.dismiss();
            try {
                if (setting.compareTo(FileManagerSettings.SETTINGS_LAYOUT_MODE) == 0) {
                    //Need to change the layout
                    getCurrentNavigationFragment().changeViewMode(NavigationLayoutMode.fromId(value));
                } else {
                    //Save and refresh
                    if (setting.getDefaultValue() instanceof Enum<?>) {
                        //Enumeration
                        Preferences.savePreference(setting, new ObjectIdentifier() {
                            @Override
                            public int getId() {
                                return value;
                            }
                        }, false);
                    } else {
                        //Boolean
                        boolean newval = Preferences.getSharedPreferences().getBoolean(setting.getId(),
                                ((Boolean) setting.getDefaultValue()).booleanValue());
                        Preferences.savePreference(setting, Boolean.valueOf(!newval), false);
                    }
                    getCurrentNavigationFragment().refresh();
                }
            } catch (Exception e) {
                Log.e(TAG, "Error applying navigation option", e); //$NON-NLS-1$
                NavigationActivity.this.mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        DialogHelper.showToast(NavigationActivity.this, R.string.msgs_settings_save_failure,
                                Toast.LENGTH_SHORT);
                    }
                });

            } finally {
                adapter.dispose();
                getCurrentNavigationFragment().getCustomTitle().restoreView();
            }

        }
    });
    popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            adapter.dispose();
        }
    });
    popup.show();
}