[Android] OnChildClickListener for ExpandableListView

[Android] OnChildClickListener for ExpandableListView

Last time we talk about how to implement the ExpandableListView widget, this time I’ll show you how to implement “OnChildClickListener” for ExpandableListView.

Please follow below codes, maybe you need to modified some codes.

public class TestExpandableListView extends Activity implements OnChildClickListener{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ExpandableListView  myListView = (ExpandableListView) findViewById(R.id. mExpandableListView);

    List<HashMap<String, String>> groupList = new ArrayList<>();
    Map<String, String> group1 = new HashMap<>();
    group1.put("group", "TEST");
    Map<String, String> group2 = new HashMap<>();
    group2.put("group","TES2");

    Map<String, String> child1Data1 = new HashMap<>();
    childData1.put("child", "childData1");
    Map<String, String> child1Data2 = new HashMap<>();
    childData2.put("child", "childData2");
    Map<String, String> child1Data3 = new HashMap<>();
    childData3.put("child", "childData3");

    List<Map<String, String>> childList1 = new ArrayList<>();
    childList1.add(child1Data1);
    childList1.add(child1Data2);
    childList1.add(child1Data3);

    Map<String, String> child2Data1 = new HashMap<>();
    childData1.put("child", "childData1");
    Map<String, String> child2Data2 = new HashMap<>();
    childData2.put("child", "childData2");
    Map<String, String> child2Data3 = new HashMap<>();
    childData3.put("child", "childData3");

    List<Map<String, String>> childList2 = new ArrayList<>();
    childList2.add(child2Data1);
    childList2.add(child2Data2);
    childList2.add(child2Data3);

    List<List<Map<String, String>>> childGroupList = new ArrayList<>();
    childGroupList.add(childList1);
    childGroupList.add(childList2);

    ExpandableAdapter viewAdapter = new ExpandableAdapter(this, groupList, childGroupList);
    myListView.setAdapter(viewAdapter);

    myListView.setOnChildClickListener(this);
    registerForContextMenu(myListView);
  }

  @Override
  public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    String eventId = ((TextView) v.findViewById(R.id.event_id)).getText().toString();
    //Do something..

    return true;
  }

  public class ExpandableAdapter extends BaseExpandableListAdapter {
    private Context context;
    List<Map<String, String>> groups;
    List<List<Map<String, String>>> children;

    public ExpandableAdapter(Context context, List<Map<String, String>> groups, List<List<Map<String, String>>> children) {
      this.groups = groups;
      this.children = children;
      this.context = context;
    }

    public Object getChild(int groupPosition, int childPosition) {
      return childs.get(groupPosition).get(childPosition);
    }

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

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      @SuppressWarnings("unchecked")
      String text = ((Map<String, String>) getChild(groupPosition, childPosition)).get("child");
      LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.child, null);
      TextView tv = (TextView) linearLayout.findViewById(R.id.child_tv);
      tv.setText(text);

      return linearLayout;
    }
    public int getChildrenCount(int groupPosition) {
      return childs.get(groupPosition).size();
    }
    public Object getGroup(int groupPosition) {
      return groups.get(groupPosition);
    }
    public int getGroupCount() {
      return groups.size();
    }
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      String text = groups.get(groupPosition).get("group");
      LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.group, null);
      TextView textView = (TextView) linearLayout.findViewById(R.id.group_tv);
      textView.setText(text);

      return linearLayout;
    }
    public boolean hasStableIds() {
      return false;
    }
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }
}

Try it!

(Visited 36 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail
Comments are closed.