Android ListView 长按列表弹出菜单
Android ListView 长按列表弹出菜单
设置长按菜单
listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, 0, 0, "删除单号"); menu.add(0, 1, 1, "接收此单"); } });
监听菜单单击事件
@Override public boolean onContextItemSelected(MenuItem item) { Log.d("TEST", item.getTitle().toString()); if(item.getMenuInfo() instanceof AdapterContextMenuInfo) { AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); Log.d("TEST", info.position+"--"); } return super.onContextItemSelected(item); }
当上下文菜单是通过AdapterView创建的,那么AdapterViewAdapterContextMenuInfo这个类可以获取当前长按的Item,可以获取长按行的id,position,targetView