简体   繁体   English

Android 兼容性上下文操作栏

[英]Android compatibility contextual action bar

In trying to follow the Android Design Guidelines, I'm running into a small quandary.在尝试遵循 Android 设计指南时,我遇到了一个小难题。

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).问题是,我试图保持向后兼容 API 7(由于我的手机目前是 2.3.3)。

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.我正在使用ActionBarSherlock来获取其他操作栏的内容,但我似乎无法弄清楚如何让它启动上下文操作栏,也没有弄清楚如何在 ABS 中任意向 ActionBar 添加按钮。 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.我看到你可以做标签,所以也许这就是答案,但由于我试图允许多选,我不想拥有正常的模式上下文菜单。

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 是一个简单的菜单,就像操作栏一样:

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

Setting up contextual actionbar is the same to setting up the 'regular' ActionBar items as far as the XML is concerned.就 XML 而言,设置上下文操作栏与设置“常规”操作栏项目相同。 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 (eg instead of Android.View.ActionMode , use com.actionbarsherlock.view.ActionMode ).为了使用 ActionBarSherlock,请将默认的 Android 回调替换为 ActionBarSherlock 编辑的回调(例如,使用com.actionbarsherlock.view.ActionMode代替Android.View.ActionMode )。

ActionBarSherlock 有自己的 ActionMode 实现,但是你必须手动控制它的生命周期,我写了一个关于这个的教程

For long click sample please refer to below links.长按示例请参考以下链接。 First one is java code required for sample.第一个是示例所需的 java 代码。 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:这是一个如何使用 ActionBarSherlock 库添加任何 View 实例(下面代码中的按钮)操作栏的示例:

@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 .基本上,您必须创建一个实现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.在每次选择(或长按)时,您使用startActionMode方法启动回调。 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编辑:/samples/demos/src/com/actionbarsherlock/sample/demos/ActionModes.java下还有一个关于 Sherlock 样本的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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