Android compatibility contextual action bar

被刻印的时光 ゝ 提交于 2019-11-28 20:22:10

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.

burak altundal

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

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