根据单击的项目对动作模式菜单进行充气



我有一个活动,在平板电脑上使用支票簿应用程序的主/细节流(左边是账户,右边是交易)。

当项目被按下时,我会使用上下文操作栏,允许用户在必要时编辑/删除项目。我喜欢这样:

@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$1.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$1.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);

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

相关内容

  • 没有找到相关文章

最新更新