根据被点击的项目膨胀动作模式菜单

Inflate action mode menu based on the item that was clicked

我有一个 activity,它在平板电脑上使用支票簿应用程序的 master/detail 流程(左侧的帐户,右侧的交易)。

当一个项目被用力按下时,我使用上下文操作栏允许用​​户在必要时 edit/delete 项目。我这样做:

@Override
public void onTransactionLongClick(Transaction t) {
    if(mActionMode == null){
        // Start the CAB using the ActionMode.Callback already defined
        mActionMode = startSupportActionMode(mActionModeCallback);
        // Get name to set as title for action bar
        mActionMode.setTitle(t.getDescription());
        // Get account ID to pass as tag.
        mActionMode.setTag(t);
    }
}

@Override
public void onAccountLongSelected(AccountPrimitives ap) {
    // Don't fire if the action mode is already active.
    if(mActionMode == null){
        // Start the CAB using the ActionMode.Callback already defined
        mActionMode = startSupportActionMode(mActionModeCallback);
        // Get name to set as title for action bar
        mActionMode.setTitle(ap.getName());
        // Get account ID to pass as tag.
        mActionMode.setTag(ap);
    }
}

如您所见,我得到了被选中的项目并将其作为标记传递给操作模式,因此我知道在必要时要开始编辑或从数据库中删除的项目。

现在我有两个窗格布局,我无法正确设置我的操作模式回调。我想做这样的事情:

private final ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            // Inflate based on tag type (account/transaction)
            if(mActionMode.getTag() instanceof AccountPrimitives){
                inflater.inflate(R.menu.account_context_menu, menu);
            } else if(mActionMode.getTag() instanceof Transaction){
                inflater.inflate(R.menu.transaction_context_menu, menu);
            }
            return true;
        }

        // Called each time the action mode is shown. Always called after onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_delete_account:
                    // The account that was selected is passed as the tag
                    // for the action mode.
                    showAccountDeleteAlertDialog((AccountPrimitives) mActionMode.getTag());
                    mode.finish(); // Action picked, so close the CAB
                    return true;
                case R.id.action_delete_transaction:
                    showTransactionDeleteAlertDialog((Transaction) mActionMode.getTag());
                    mode.finish();
                    return true;
                default:
                    return false;
            }
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null;
        }
    };

但是,我得到一个空指针异常,因为在我调用mActionMode.setTag()之前动作模式已经启动,所以我无法确定它的类型。在 onXLongClick 方法中,我无法将 setTag() 移动到第一行,因为我也得到了 NPE。

如何设置条件来为操作模式扩展特定菜单?

编辑

当我长按一个项目时,出现以下异常:

04-24 17:45:22.441    8117-8117/com.example.android.cashcaretaker E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.android.cashcaretaker, PID: 8117
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.support.v7.view.ActionMode.getTag()' on a null object reference
            at com.example.android.cashcaretaker.AccountsActivity.onCreateActionMode(AccountsActivity.java:58)
            at android.support.v7.app.ActionBarActivityDelegateBase$ActionModeCallbackWrapper.onCreateActionMode(ActionBarActivityDelegateBase.java:1451)
            at android.support.v7.internal.app.WindowDecorActionBar$ActionModeImpl.dispatchOnCreate(WindowDecorActionBar.java:1015)
            at android.support.v7.internal.app.WindowDecorActionBar.startActionMode(WindowDecorActionBar.java:510)
            at android.support.v7.app.ActionBarActivityDelegateBase.startSupportActionMode(ActionBarActivityDelegateBase.java:570)
            at android.support.v7.app.ActionBarActivity.startSupportActionMode(ActionBarActivity.java:225)
            at com.example.android.cashcaretaker.AccountsActivity.onTransactionLongClick(AccountsActivity.java:177)
            at com.example.android.cashcaretaker.TransactionFragment.onItemLongClick(TransactionFragment.java:72)
            at android.widget.AbsListView.performLongPress(AbsListView.java:3121)
            at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3070)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我对账户和交易都得到了相同的异常,唯一的区别是调用它们的方法。

我解决这个问题的方法是创建第二个 ActionMode.Callback 对象。我做的第一个是处理账户的,第二个是处理交易的。

它们每个看起来都像上面的那个,我可以这样称呼它们:

// In Account long click
mActionMode = startSupportActionMode(mAccountActionModeCallback);

// In transaction long click
mActionMode = startSupportActionMode(mTransactionActionModeCallback);

然后,在每个回调中,我都能够扩充必要的菜单并处理必要的操作项。