android ExpandableListView 为Group与Child添加菜单

it2022-05-05  107

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

1、通过ShowDialog

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

代码如下:

@Override      public View getChildView(final int arg0, final int arg1, boolean arg2, View view,              ViewGroup arg4) {          if(view==null){              view = inflater.inflate(R.layout.team_layout_main_list_item, null);          }          TextView textView = (TextView) view.findViewById(R.id.team_main_list_username);          ImageView imageView = (ImageView) view.findViewById(R.id.team_main_list_head);          imageView.setImageResource(PrortaitUtils.conversionIdToRes(((UserInfo)getChild(arg0, arg1)).getPortrait()));          view.setTag(R.id.team_singlechat_id_send, arg0);          view.setTag(R.id.team_singlechat_id_close, arg1);       //备注1          textView.setText(((UserInfo)getChild(arg0, arg1)).getUsername());          textView.setTextColor(Color.BLACK);          view.setBackgroundColor(Color.rgb(247243247));          view.setBackgroundResource(R.drawable.team_item_bg_selector);          return view;      }  

 

@Override      public View getGroupView(int arg0, boolean arg1, View groupView, ViewGroup arg3) {          if(groupView == null){              groupView = inflater.inflate(R.layout.team_layout_main_list_group, null);          }          TextView groupTextView = (TextView) groupView.findViewById(R.id.team_main_list_teamname);          ImageView groupImageView = (ImageView) groupView.findViewById(R.id.team_main_list_into);          groupImageView.setImageResource(R.drawable.team_into1);          if(arg1){              groupImageView.setImageResource(R.drawable.team_into2);          }          groupView.setTag(R.id.team_singlechat_id_send, arg0);          groupView.setTag(R.id.team_singlechat_id_close, -1);        //备注2          groupTextView.setText(getGroup(arg0).toString());          groupTextView.setTextColor(Color.BLACK);          return groupView;      }  

 

//长按条目,包括group和child      onLineList.setOnItemLongClickListener(new OnItemLongClickListener() {            @Override          public boolean onItemLongClick(AdapterView<?> arg0, final View arg1,                  int arg2, long arg3) {              longClickListIteam(arg1);              return true;          }      });       /**       * 长按list中的某一个条目       */       private void longClickListIteam(final View arg1) {              final int groupPos = (Integer)arg1.getTag(R.id.team_singlechat_id_send);                  final int childPos = (Integer)arg1.getTag(R.id.team_singlechat_id_close);              Log.i(TAG,"groupPos:"+groupPos+",childPos:"+childPos);              if(childPos == -1){         //如果得到child位置的值为-1,则是操作group                  Log.i(TAG,"操作group组件");                  updateTeam(arg1);              }else{                  Log.i(TAG,"操作child组件");     //否则,操作child                  final List<UserInfo> users = getTeamUser(groupPos);                  //判断长按的是否为自己,如果是不是自己,则弹出窗口操作,如果是自己,则查看资料                  if(DicqConstant.DEFAULTMAC.equalsIgnoreCase(users.get(childPos).getUsermac())){                      Intent it = new Intent(TeamMainActivity.this,UserInfoActivity.class);                      it.putExtra("isMyself"true);                      TeamMainActivity.this.startActivity(it);                  }else{                      builder = new AlertDialog.Builder(TeamMainActivity.this);                      builder.setTitle("操作用户");                      builder.setItems(new String[]{"查看资料","查看共享","移至分组","权限设置"}, new DialogInterface.OnClickListener() {                          public void onClick(DialogInterface dialog, int which) {                              if(which == 0){                                  Intent it = new Intent(TeamMainActivity.this,UserInfoActivity.class);                                  it.putExtra("isMyself"false);                                  it.putExtra("userinfo", users.get(childPos));                                  TeamMainActivity.this.startActivity(it);                              }else if(which == 1){                                  Intent it1 = new Intent(TeamMainActivity.this,FileShareActivity.class);                                  it1.putExtra("userinfo",users.get(childPos));                                  TeamMainActivity.this.startActivity(it1);                              }else if(which == 2){                                  updateChild(groupPos,childPos);                              }else if(which == 3){                                  updateUserPrivilege(groupPos,childPos);                              }                          }                      }).show();                  }                }          }  

第二种方式、是能ContextMenu。

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

代码如下:

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

转载于:https://www.cnblogs.com/lslzmx/archive/2013/05/28/3103798.html

相关资源:各显卡算力对照表!

最新回复(0)