Android: long click on the child views of a ExpandableListView?

前端 未结 5 705
星月不相逢
星月不相逢 2020-12-02 06:41

ExpandableListView has a setOnChildClickListener method, but lacks of setOnChildLongClickListener method.

When I added setOnL

5条回答
  •  借酒劲吻你
    2020-12-02 07:27

    I handle ur problem. just set a tag ur item and use it ,like this:

    adapter = new ExpandableListAdapter() {
    
    
            private String[] groups = { "Biceps", "Deltoids", "Hamstrings", "Lower Back","Quadriceps","Triceps","Wrist" };
            private String[][] children = {
                    { "Konsantra Dumbell curl", "Hammer curl", "Barbell Biceps Curl", "Prone Curl" },
                    { "Arnold Press", "Lateral Raise", "Dumbell Upright row", "Bar Military Press" },
                    { "Dead Lift", "Hack Squat","Zercher Squat","Seated Leg Flexion" },
                    { "Back Raise", "Superman" },
                    { "Back Squat", "Bulgarian Split Squat","Dumbell Lunge" },
                    { "Bench Dip", "French Press","Triceps Extension" },
                    { "Dumbell Wrist Curl "," Reverse Writst Curl"}
            };
            public void unregisterDataSetObserver(DataSetObserver observer) {
                // TODO Auto-generated method stub
    
            }
    
            public void registerDataSetObserver(DataSetObserver observer) {
                // TODO Auto-generated method stub
    
            }
    
            public void onGroupExpanded(int groupPosition) {
                // TODO Auto-generated method stub
    
            }
    
            public void onGroupCollapsed(int groupPosition) {
                // TODO Auto-generated method stub
    
            }
    
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                return false;
            }
    
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return true;
            }
    
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return true;
            }
    
            public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {
                 TextView textView = getGenericView();
                    textView.setText(getGroup(groupPosition).toString());
                    textView.setTag((Object)getGroup(groupPosition).toString()+"G");
                    return textView;
            }
    
            public long getGroupId(int groupPosition) {
                // TODO Auto-generated method stub
                return groupPosition;
            }
    
            public int getGroupCount() {
                // TODO Auto-generated method stub
                return groups.length;
            }
    
            public Object getGroup(int groupPosition) {
                // TODO Auto-generated method stub
                return groups[groupPosition];
            }
    
            public long getCombinedGroupId(long groupId) {
                // TODO Auto-generated method stub
                return 0;
            }
    
            public long getCombinedChildId(long groupId, long childId) {
                // TODO Auto-generated method stub
                return 0;
            }
    
            public int getChildrenCount(int groupPosition) {
                // TODO Auto-generated method stub
                return children[groupPosition].length;
            }
    
            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {
                 TextView textView = getGenericView();
                    textView.setText(getChild(groupPosition, childPosition).toString());
                    textView.setTag((Object)getChild(groupPosition, childPosition).toString()+"C");
                    return textView;
            }
    
            public long getChildId(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return childPosition;
            }
    
            public Object getChild(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return children[groupPosition][childPosition];
            }
    
            public boolean areAllItemsEnabled() {
                // TODO Auto-generated method stub
                return false;
            }
    
            public TextView getGenericView() {
                // Layout parameters for the ExpandableListView
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.FILL_PARENT, 64);
    
                TextView textView = new TextView(expandXml.this);
                textView.setLayoutParams(lp);
                // Center the text vertically
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                // Set the text starting position
                textView.setPadding(36, 0, 0, 0);
    
                return textView;
            }
    
        };
    

    And then setOnItemLongClickListener for Expandable ListView.

    listView.setOnItemLongClickListener(new OnItemLongClickListener() {
    
            public boolean onItemLongClick(AdapterView arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                String s=null;
    
                    int childPosition =ExpandableListView.getPackedPositionChild(arg3);
                    if(arg1.getTag()!=null){
                    Object o= arg1.getTag();
    
                     s = o.toString();
                    }
                    Toast.makeText(expandXml.this ,s , Toast.LENGTH_SHORT).show();
    
                return false;
            }
        });
    

    Here we go.

提交回复
热议问题