Android compatibility contextual action bar

扶醉桌前 提交于 2019-11-27 13:03:31

问题


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.


回答1:


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).




回答2:


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" />




回答3:


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




回答4:


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;

  • Java source
  • Layout xml



回答5:


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);
}



回答6:


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



来源:https://stackoverflow.com/questions/9538128/android-compatibility-contextual-action-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!