我正在尝试使用操作模式获取上下文菜单,我可以通过长按它来选择 ListView 中的项目。我根据此参考创建了一个 MultiChoiceListener并制作了一个 ItemLongClickListener ,它将一个项目设置为已选中,这是动作模式根据此参考工作所需的。
我的问题是,即使正在播放项目长按动画,ActionMenu 也不会膨胀。
列表视图代码:
final ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
listView.setLongClickable(true);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
Cursor cursor = db.getSubject(id);
String subject = null;
try {
subject = cursor.getString(cursor.getColumnIndex("subject"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(Main.this, Marks.class);
intent.putExtra("selected", subject);
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
listView.setItemChecked(arg2, true);
return true;
}
});
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
}
});
提前致谢!