我有ListView
显示自定义视图(每个项目3 TextViews
);我刚刚实现了这样的上下文菜单:
// in onCreateView
ListView list = (ListView) view.findViewById(R.id.list);
registerForContextMenu(list);
和
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.list) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.todo_context_menu, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
// your first action code
return true;
case R.id.delete:
// your second action code
return true;
default:
return super.onContextItemSelected(item);
}
}
我希望知道哪个项目已被长时间点击(它在ListView
中的位置是理想的),以便编辑或删除正确的项目。
怎么可能实现?
答案 0 :(得分:0)
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
switch (item.getItemId()) {
case R.id.edit:
// your first action code
return true;
case R.id.delete:
// your second action code
return true;
default:
return super.onContextItemSelected(item);
}
}