简体   繁体   中英

How to close the text selection contextual action bar programmatically?

EDIT:

Initial problem:

I have a fragment that extends a DialogFragment. In this fragment I create an AlertDialog and set an adapter. The adapter uses a custom layout to make the TextViews selectable.

Now it's all fine and dandy but after selecting some text and dismissing the dialog, later the text is selected but the action bar is not shown anymore. It can be fixed only by re-creating the activity which starts the fragment.

tl;dr : Using a static dialog. After text selection and dismiss() , the text can be selected but the CAB is nowhere to be found. Perhaps closing the CAB programatically can solve this. How would I do that?

General question:

I would like to find out how I can manually close the default android text selection CAB (or get a reference to it).

I solved my initial problem by creating a new dialog for each fragment. Using a static dialog somehow messed up my text selection. It's too bad though as I now have to do a bunch of adjustments everytime I start the DialogFragment.

Now for my general question I solved that by setting ActionMode callbacks to each convertView of my adapter. That way I was able to close the ActionMode anytime between getting the reference to the mode ( onCreateActionMode ) and clearing the reference ( onDestroyActionMode ). Here's the code:

public ActionMode mActionMode;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // (Re)Use the convertView
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.popup_list_item, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.popupItem);
        holder.textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mActionMode = mode;
                // Can now use the mode whenever (if it's not null)
                // e.g. call mActionMode.finish()
                return true; // true = create the ActionMode
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                mActionMode = null;
            }
        });
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Set text
    if (mQuery != null)
        holder.textView.setText(Html.fromHtml(getItem(position)));
    else
        holder.textView.setText(getItem(position));

    return convertView;
}

However I failed to solve my initial problem (even after being able to close the ActionMode manually), so I was forced to drop the usage of a static Dialog.

I am open to suggestions, on how to solve my initial problem, if anyone has any.

This question would be easier to answer if you had shown us the code with which you open the CAB, but in general it works like this:

After you implemented your ActionMode.Callback you open the CAB like this:

getActivity().startActionMode(new ActionModeCallbackImpl());

But startActionMode() returns an ActionMode object which you can save in a variable:

ActionMode actionMode = getActivity().startActionMode(new ActionModeCallbackImpl());

With this ActionMode object you can later on close the CAB` like this:

actionMode.finish();

I hope I could help you and if you have any other questions please feel free to ask!

i had same issue. i dont wanted CAB to Appear so i styled it off.

Check this answere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM