سؤال

In trying to follow the Android Design Guidelines, I'm running into a small quandary.

I want to have a list of items that I can long-press several of (multi-select), and then perform bulk actions on them.

The Design Guidelines suggest using the Contextual Action Bar for this, and it sounds perfectly like what I had in mind. Problem is, I'm trying to maintain compatibility backwards to API 7 (due to my phone being 2.3.3 currently).

I'm using ActionBarSherlock to get other actionbar stuff, but I can't seem to figure out how to get it to either fire up a contextual action bar, nor have I figured out how to add buttons arbitrarily to the ActionBar in ABS. I see you can do tabs, so maybe that's the answer, but since I'm trying to allow multi-select, I don't want to have the normal modal context menu.

هل كانت مفيدة؟

المحلول

Setting up contextual actionbar is the same to setting up the 'regular' ActionBar items as far as the XML is concerned. This example in the developer's guide explains it all.

In order to use ActionBarSherlock, replace the default Android-callbacks to the ActionBarSherlock-edited callbacks (e.g. instead of Android.View.ActionMode, use com.actionbarsherlock.view.ActionMode).

نصائح أخرى

This is a late answer, but I think would help people stuck.

Opening the contextual action bar is actually pretty simple, at any point in your activity you just have to call:

startActionMode(mActionModeCallback);

If you are not in your main activity, like in fragments, you can get a reference with

getSherlockActivity().startActionMode(mActionModeCallback);

and this is the callback

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback(){

    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
          MenuInflater inflater = mode.getMenuInflater();
          inflater.inflate(R.menu.actionbar_context_menu, menu);
          return true;
        }

    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item1:
                return true;
            case R.id.menu_item2:
                //close the action mode
                //mode.finish();
                return true;
            default:
                mode.finish();
                return false;
       }
    }
};

The xml is a simple menu like the actionbar one:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/menu_item1"
      android:icon="@drawable/ic_item1"
      android:title="@string/ITEM1"
      android:showAsAction="always|withText" />

<item android:id="@+id/menu_item2"
      android:icon="@drawable/ic_item2"
      android:title="@string/ITEM2"
      android:showAsAction="always|withText" />

ActionBarSherlock has its own implementation of ActionMode, but you'll have to manualy controll its lifesycle, I wrote a tutorial about this.

For long click sample please refer to below links. First one is java code required for sample. And second one is how to define the layout;

I will answer second part of your question. Here is an example how to add any View instance (button in the code below) actionbar with ActionBarSherlock library:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        refreshButton = (RotatingButton) LayoutInflater.from(this).inflate(R.layout.actionbar_customview_refresh, null);
        refreshButton.setOnClickListener(refreshButtonListener);

        MenuItem item = menu.add(0, android.R.id.copy, 0, getString(R.string.actionbar_refresh));
        item.setActionView(refreshButton);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_action_bar, menu);
}

I was facing the same issue. It was solved when I found this link. Basically, you have to create a callback class that implements ActionMode.Callback. In this class, you inflate the Action Bar with your contextual Action Bar. At each selection (or long click), you start the callback using the startActionMode method. See the link for an working code =]

EDIT: There is also an example on Sherlock's samples under /samples/demos/src/com/actionbarsherlock/sample/demos/ActionModes.java

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top