我正在尝试通过 ActionMode 在 ActionBar 中显示菜单项,但 ActionMode 和菜单项均未显示。
在 ProvasActivity 上制作我的 ListView 的 LongClickListener。
this.listView.setLongClickable(true);
this.listView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(null, "ListView Pressionado");
// O action mode está ativado no momento?
if (mActionMode != null) {
// Cancela o evento
return false;
}
selectedItem = position;
mActionMode = ProvasActivity.this
.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Infla o recurso do menu a ser exibido
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate( R.menu.linha_selecionada, menu );
Log.d(null, "Menu Inflado");
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_remover:
show();
// Fecha a Action quando executada
mode.finish();
break;
default:
break;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Anula o ActionMode
mActionMode = null;
selectedItem = -1;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
private void show() {
Toast.makeText(ProvasActivity.this,
"show", Constants.TEMPO_TOAST).show();
Log.d(null, "show()");
}
};
我的菜单用于显示 ActionMode 项(布局/菜单/linha_selecionada):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_remover"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/amazenadas"/>
</menu>
我的 ProvasActivity (layout/provas_armzenadas.xml) 的视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical"
android:paddingTop="50dp" >
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:dividerHeight="4px"/>
</RelativeLayout>
我正在尝试完成 Vogella 教程。
http://www.vogella.com/tutorials/AndroidListView/article.html