Android ListView 长按列表弹出菜单

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, "接收此单");   
   }
});
Android ListView 长按列表弹出菜单

 

监听菜单单击事件

Android ListView 长按列表弹出菜单
    @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);  
    } 
Android ListView 长按列表弹出菜单


当上下文菜单是通过AdapterView创建的,那么AdapterViewAdapterContextMenuInfo这个类可以获取当前长按的Item,可以获取长按行的id,position,targetView

Android ListView 长按列表弹出菜单

Android ListView 长按列表弹出菜单,布布扣,bubuko.com

Android ListView 长按列表弹出菜单

上一篇:Android fill_parent、wrap_content和match_parent的区别


下一篇:android draw() onDraw() dispathDraw() onDrawScrollBars