private MenuItem mDeleteItem;
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
/// M: Optimize select all performance, restore actionmode status. @{
mListAdapter.clearstate();
// M: add for ALPS01988446, dismiss search view.
mSearchItem.collapseActionView();
if (mIsNeedRestoreAdapterState) {
for (int i = 0; i < mListSelectedThreads.length; i++) {
mListAdapter.setSelectedState(mListSelectedThreads[i]);
}
mIsNeedRestoreAdapterState = false;
} else {
Log.d(TAG, "onCreateActionMode: no need to restore adapter state");
}
/// @}
mSelectedThreadIds = new HashSet<Long>();
inflater.inflate(R.menu.conversation_multi_select_menu_with_selectall, menu);
final Menu m=menu;
mDeleteItem = menu.findItem(R.id.delete);
mDeleteItem.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//調用delete的方法,具體實現點擊效果
m.performIdentifierAction(mDeleteItem.getItemId(), 0);
}
});
@Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
if (mSelectedThreadIds.size() > 0) {
Log.v(TAG, "ConversationList->ModeCallback: delete");
if (mDeleteAlertDialog != null && mDeleteAlertDialog.isShowing()) {
MmsLog.d(TAG, "no need to show delete dialog");
} else {
confirmDeleteThreads(mSelectedThreadIds, mQueryHandler);
}
} else {
item.setEnabled(false);
}
break;
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/delete"
android:title="@string/delete"
android:actionLayout="@layout/zzz_menu_actionlayout"
android:showAsAction="always" />
</menu>
具體的佈局zzz_menu_actionlayout<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="10dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@drawable/ic_menu_trash_holo_dark"
android:background="?android:attr/selectableItemBackground"
/>
</LinearLayout>
這樣就可以實現menuItem的偏移問題