android ExpandableListView 为Group与Child添加菜单

为ExpandableListVIew的Group与Child添加不同菜单有两种方式:

1、通过ShowDialog

这种方式是通过适配器为VIew添加Tag然后在ExpandableListView的OnItemLongClickListener获取View的Tag来判断是Group或Child,然后弹出不同的菜单对话框。

代码如下:

  1. @Override  
  2.     public View getChildView(final int arg0, final int arg1, boolean arg2, View view,  
  3.             ViewGroup arg4) {  
  4.         if(view==null){  
  5.             view = inflater.inflate(R.layout.team_layout_main_list_item, null);  
  6.         }  
  7.         TextView textView = (TextView) view.findViewById(R.id.team_main_list_username);  
  8.         ImageView imageView = (ImageView) view.findViewById(R.id.team_main_list_head);  
  9.         imageView.setImageResource(PrortaitUtils.conversionIdToRes(((UserInfo)getChild(arg0, arg1)).getPortrait()));  
  10.         view.setTag(R.id.team_singlechat_id_send, arg0);  
  11.         view.setTag(R.id.team_singlechat_id_close, arg1);       //备注1  
  12.         textView.setText(((UserInfo)getChild(arg0, arg1)).getUsername());  
  13.         textView.setTextColor(Color.BLACK);  
  14.         view.setBackgroundColor(Color.rgb(247243247));  
  15.         view.setBackgroundResource(R.drawable.team_item_bg_selector);  
  16.         return view;  
  17.     }  

 

  1. @Override  
  2.     public View getGroupView(int arg0, boolean arg1, View groupView, ViewGroup arg3) {  
  3.         if(groupView == null){  
  4.             groupView = inflater.inflate(R.layout.team_layout_main_list_group, null);  
  5.         }  
  6.         TextView groupTextView = (TextView) groupView.findViewById(R.id.team_main_list_teamname);  
  7.         ImageView groupImageView = (ImageView) groupView.findViewById(R.id.team_main_list_into);  
  8.         groupImageView.setImageResource(R.drawable.team_into1);  
  9.         if(arg1){  
  10.             groupImageView.setImageResource(R.drawable.team_into2);  
  11.         }  
  12.         groupView.setTag(R.id.team_singlechat_id_send, arg0);  
  13.         groupView.setTag(R.id.team_singlechat_id_close, -1);        //备注2  
  14.         groupTextView.setText(getGroup(arg0).toString());  
  15.         groupTextView.setTextColor(Color.BLACK);  
  16.         return groupView;  
  17.     }  

 

  1. //长按条目,包括group和child  
  2.     onLineList.setOnItemLongClickListener(new OnItemLongClickListener() {  
  3.   
  4.         @Override  
  5.         public boolean onItemLongClick(AdapterView<?> arg0, final View arg1,  
  6.                 int arg2, long arg3) {  
  7.             longClickListIteam(arg1);  
  8.             return true;  
  9.         }  
  10.     });  
  11.      /** 
  12.       * 长按list中的某一个条目 
  13.       */  
  14.      private void longClickListIteam(final View arg1) {  
  15.             final int groupPos = (Integer)arg1.getTag(R.id.team_singlechat_id_send);      
  16.             final int childPos = (Integer)arg1.getTag(R.id.team_singlechat_id_close);  
  17.             Log.i(TAG,"groupPos:"+groupPos+",childPos:"+childPos);  
  18.             if(childPos == -1){         //如果得到child位置的值为-1,则是操作group  
  19.                 Log.i(TAG,"操作group组件");  
  20.                 updateTeam(arg1);  
  21.             }else{  
  22.                 Log.i(TAG,"操作child组件");     //否则,操作child  
  23.                 final List<UserInfo> users = getTeamUser(groupPos);  
  24.                 //判断长按的是否为自己,如果是不是自己,则弹出窗口操作,如果是自己,则查看资料  
  25.                 if(DicqConstant.DEFAULTMAC.equalsIgnoreCase(users.get(childPos).getUsermac())){  
  26.                     Intent it = new Intent(TeamMainActivity.this,UserInfoActivity.class);  
  27.                     it.putExtra("isMyself"true);  
  28.                     TeamMainActivity.this.startActivity(it);  
  29.                 }else{  
  30.                     builder = new AlertDialog.Builder(TeamMainActivity.this);  
  31.                     builder.setTitle("操作用户");  
  32.                     builder.setItems(new String[]{"查看资料","查看共享","移至分组","权限设置"}, new DialogInterface.OnClickListener() {  
  33.                         public void onClick(DialogInterface dialog, int which) {  
  34.                             if(which == 0){  
  35.                                 Intent it = new Intent(TeamMainActivity.this,UserInfoActivity.class);  
  36.                                 it.putExtra("isMyself"false);  
  37.                                 it.putExtra("userinfo", users.get(childPos));  
  38.                                 TeamMainActivity.this.startActivity(it);  
  39.                             }else if(which == 1){  
  40.                                 Intent it1 = new Intent(TeamMainActivity.this,FileShareActivity.class);  
  41.                                 it1.putExtra("userinfo",users.get(childPos));  
  42.                                 TeamMainActivity.this.startActivity(it1);  
  43.                             }else if(which == 2){  
  44.                                 updateChild(groupPos,childPos);  
  45.                             }else if(which == 3){  
  46.                                 updateUserPrivilege(groupPos,childPos);  
  47.                             }  
  48.                         }  
  49.                     }).show();  
  50.                 }  
  51.   
  52.             }  
  53.         }  

第二种方式、是能ContextMenu。

这种方式是通过把ExpandableListContextMenuInfo的Type来判断是Group或Child,然后创建不同的菜单

代码如下:

  1. @Override
  2.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  3.             ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
  4.             int type = ExpandableListView.getPackedPositionType(info.packedPosition); 
  5.             int group = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
  6.             if(type == ExpandableListView.PACKED_POSITION_TYPE_CHILD && group!=0){
  7.                     menu.setHeaderTitle("Sample menu");
  8.                     menu.add(0, 0, 0, R.string.expandable_list_sample_action);
  9.             }
  10.     }

posted on 2013-05-28 15:57  森 林  阅读(452)  评论(0编辑  收藏  举报

导航