setOnGroupClickListener在ExpandableListView的SecondLevelAdapter上不起作用

时间:2019-01-10 06:03:43

标签: android expandablelistview

我需要通过单击SecondLevel iteself的GroupItem来进入另一个活动。我需要通过setOnGroupClickListener ExpandableListView获得第二级的GroupItem。但是我正在使用OnChildClickListener,而不是在OnGroupClickListener中。请查看我的代码,如果可以的话,请给我一个解决方案,请帮助我,谢谢。

 public class ThreeLevelListAdapter extends BaseExpandableListAdapter {

String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;
String grandChildItem;
String childItem;
String childItem2;
String parentItem;

/**
 * Constructor
 * @param context
 * @param parentHeader
 * @param secondLevel
 * @param data
 */
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
    this.context = context;
    this.parentHeaders = parentHeader;
    this.secondLevel = secondLevel;
    this.data = data;

}
@Override
public int getGroupCount() {
    return parentHeaders.length;
}
@Override
public int getChildrenCount(int groupPosition) {
    // no idea why this code is working
    return 1;
}

@Override
public Object getGroup(int i) {
    return null;
}

public Object getGroup(String[] groupPosition) {
    return groupPosition;
}
@Override
public Object getChild(int group, int child) {
    return child;
}
@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}
@Override
public boolean hasStableIds() {
    return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.row_first, null);
    TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
    text.setText(this.parentHeaders[groupPosition]);
    return convertView;

}
@Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
    final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);
    String[] headers = secondLevel.get(groupPosition);
    List<String[]> childData = new ArrayList<>();
    HashMap<String, String[]> secondLevelData = data.get(groupPosition);
    for (String key : secondLevelData.keySet()) {
        childData.add(secondLevelData.get(key));
    }
    secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));
    secondLevelELV.setGroupIndicator(null);
    secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        int previousGroup = -1;
        @Override
        public void onGroupExpand(int groupPosition) {
            if (groupPosition != previousGroup)
                secondLevelELV.collapseGroup(previousGroup);
            previousGroup = groupPosition;
        }
    });
    secondLevelELV.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            int ppos = (int)expandableListView.getTag();

            parentItem = (String)getGroup(ppos);
            SecondLevelAdapter adapter = (SecondLevelAdapter)expandableListView.getExpandableListAdapter();
            childItem = (String)adapter.getGroup(groupPosition);
            Toast.makeText(parent.getContext(), childItem,Toast.LENGTH_SHORT).show();

            return true;
        }
    });
    secondLevelELV.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
            int ppos = (int)expandableListView.getTag();

            parentItem = (String)getGroup(ppos);
            SecondLevelAdapter adapter = (SecondLevelAdapter)expandableListView.getExpandableListAdapter();
            childItem = (String)adapter.getGroup(i);

            grandChildItem = (String)adapter.getChild(i, i1);

            Toast.makeText(parent.getContext(), grandChildItem,Toast.LENGTH_SHORT).show();
            dataItems();
        return true;
        }
        private void dataItems() {
            if(grandChildItem=="Living Room Sets"){
                Intent intent = new Intent(parent.getContext(),Particular_Product.class);
                context.startActivity(intent);
            }

        }
    });
    secondLevelELV.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
           childItem2 =(String)getGroup(groupPosition);
           Toast.makeText(parent.getContext(), childItem2,Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    secondLevelELV.setTag(groupPosition);
    return secondLevelELV;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

}

0 个答案:

没有答案