//注意,listview中的子布局一般优先获得焦点,尤其是在子布局里含有button控件时候,button自动抢占焦点。此时对listview的长按弹出对话框应该放在适配器里对item监听
对一个相对布局长按弹出对话框
rl.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View view , ContextMenuInfo info) {
menu.add(0, 0, 0, R.string.deleteRecordItem);
menu.add(0, 1, 0, R.string.clearAllRecod);
/* Add as many context-menu-options as you want to. */
}
});
点击之后的响应方法
@Override
public boolean onContextItemSelected(MenuItem aItem) {ContextMenuInfo menuInfo = (ContextMenuInfo) aItem.getMenuInfo();
/* Switch on the ID of the item, to get what the user selected. */
switch (aItem.getItemId()) {
case 0:
deleteFile(list.get(posItem));
list.remove(posItem);
adapter.notifyDataSetChanged();
break;
case 1:
for (int i = 0; i < list.size(); i++) {
deleteFile(list.get(i));
}
list.clear();;
adapter.notifyDataSetChanged();
break;
default:
break;
}
return false;
}