contextual action mode in fragment - close if not focused?

前端 未结 5 805
小蘑菇
小蘑菇 2021-02-04 15:08

i implemented a contextual action mode bar in a nested fragement. This fragment is part of a view pager and the view pager is also a fragment and part of a navigation drawer.

5条回答
  •  别跟我提以往
    2021-02-04 15:46

    I combined code from the OP with the override suggested by user pomber:

    • I save the ActionMode to a class field when the action mode is created.
    • I set the field back to null when the action mode is destroyed.
    • I override setUserVisibleHint in my fragment and call ActionMode#finish() if the fragment isn't visible in the view pager.
    • I also call ActionMode#finish() in the onPause() method of the fragment to close the fragment if the user navigates elsewhere.

    Code:

    @Nullable
    ActionMode mActionMode = null;
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        // Start out with a progress indicator.
        setListShownNoAnimation(false);
    
        setEmptyText(getText(R.string.no_forms_in_progress));
    
        getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
        getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                // Here you can do something when items are selected/de-selected,
                // such as update the title in the CAB
            }
    
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.context_saved_item, menu);
                inflater.inflate(R.menu.context_instance_list, menu);
                mActionMode = mode;
                return true;
            }
    
            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }
    
            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_delete:
                        confirmDelete(getListView().getCheckedItemIds());
                        return true;
                    case R.id.action_print:
                        print(getListView().getCheckedItemIds());
                        return true;
                }
                return false;
            }
    
            @Override
            public void onDestroyActionMode(ActionMode mode) {
                mActionMode = null;
            }
        });
    }
    
    @Override
    public void onPause() {
        super.onPause();
        if (mActionMode != null) {
            mActionMode.finish();
        }
    }
    
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mActionMode != null && !isVisibleToUser) {
            mActionMode.finish();
        }
    }
    

提交回复
热议问题