展开列表不会在单击时展开

时间:2016-02-29 09:12:22

标签: android

我正在尝试扩展可扩展标题视图的列表onclick。但是我被卡住了为什么我无法显示子列表。

这是我的活动课。

adapter = new    MyExpandableAdapter(getActivity(),parentHashMap,parentObjectCollection);
        expandableListView.setAdapter(adapter)
        // Listview Group expanded listener
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getActivity(),        parentObjectCollection.get(groupPosition) + " Expanded", Toast.LENGTH_SHORT).show();
            }
        });

适配器类是:

 public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> parentObjectsCollection;
private HashMap<String, List<MarkerInfoData>> parentHashMap;
/* ArrayList<MarkerInfoData> child_list;
LayoutInflater inflater;*/

public MyExpandableAdapter(Context context, HashMap<String, List<MarkerInfoData>> hashmap, List<String> list) {
    parentHashMap = hashmap;
    this.context = context;
    this.parentHashMap = hashmap;
    this.parentObjectsCollection = list;
}


@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    //String childTitle = (String) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_row, parent, false);
    }
    TextView cityName = (TextView) convertView.findViewById(R.id.txtLocationcityName);
    TextView cityAddress = (TextView) convertView.findViewById(R.id.txtLocationcityAddress);
    TextView cityDistance = (TextView) convertView.findViewById(R.id.txtcityLocationDistanceFromCurrentLocation);
    cityName.setText(parentHashMap.get(parentObjectsCollection.get(groupPosition)).get(childPosition).markerName);
    cityAddress.setText(parentHashMap.get(parentObjectsCollection.get(groupPosition)).get(childPosition).markerAddress);
    String distanceInMiles = parentHashMap.get(parentObjectsCollection.get(groupPosition)).get(childPosition).distance;
    if (distanceInMiles != null && distanceInMiles.length() > 0) {
        cityDistance.setText(distanceInMiles + "\nMiles");
    } else {
        cityDistance.setText("");
    }
    //childtextview.setText(childTitle);

    return convertView;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String GroupTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.parent_row, parent, false);
    }
    TextView parenttextview = (TextView) convertView.findViewById(R.id.textviewparent);
    parenttextview.setText(GroupTitle);
    return convertView;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.parentHashMap.get(this.parentObjectsCollection.get(groupPosition)).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public int getChildrenCount(int groupPosition) {
     return this.parentHashMap.get(this.parentObjectsCollection.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this.parentObjectsCollection.get(groupPosition);
}

@Override
public int getGroupCount() {
      return parentObjectsCollection.size();
}


@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}


@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

2 个答案:

答案 0 :(得分:0)

我认为你的代码中缺少onGroupClick方法,如果处理了click,它应返回true。

答案 1 :(得分:0)

试试这个

  expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Toast.makeText(MainActivity.this,headers.get(groupPosition)+"--"+headeritems.get(headers.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show();
            return false;
        }
    });

请参阅此处: - http://coderzpassion.com/tag/expandablelistview/