//注意,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;       

   }


更多相关文章

  1. 安卓开发06:布局-线性布局 LinearLayout
  2. Android(安卓)对话框AlertDialog和AlertDialog.Builder两者的区
  3. 【Android开发】布局管理器-相对布局
  4. ViewPager And Fragment
  5. 将Android(安卓)Studio默认布局ConstraintLayout切换成LinearLay
  6. android 纯代码 详细编写布局文件
  7. Android中的RecyclerView源码分析
  8. android实现点击按钮切换不同的fragment布局
  9. 让用户不再遭遇ANR

随机推荐

  1. Oracle ADDM --dbms_addm执行oracle数据
  2. 【DB笔试面试726】在Oracle中,RAC中的Publ
  3. 【DB笔试面试739】在Oracle中,如何获取集
  4. 【DB笔试面试602】在Oracle中,如何从执行
  5. 【DB笔试面试756】在Oracle的DG中,有哪些
  6. Oracle 定义者权限与调用者权限(AUTHID CU
  7. 【DB笔试面试591】在Oracle中,什么是星型
  8. 【DB笔试面试601】在Oracle中,给出下面执
  9. Oracle的簇与簇表
  10. 【DB笔试面试765】在Oracle中,什么是OGG?它