Expandablelistview Example

Posted on

Example of using ExpandableListView:


Create /res/layout/group_layout.xml, to define the layout of listing group:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:background="#A0A0A0"     android:orientation="horizontal" >       <ImageView         android:id="@+id/groupimg"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/ic_launcher" />     <TextView         android:id="@+id/group"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:textColor="@android:color/white"         android:textStyle="bold" />   </LinearLayout> 

Create /res/layout/item_layout.xml to define layout of items:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="55dip"     android:orientation="vertical" >       <TextView         android:id="@+id/item"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:textStyle="italic" />   </LinearLayout> 

/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.example.androidexpandablelistview.MainActivity" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <ExpandableListView         android:id="@+id/mylist"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </LinearLayout> 

Create MyExpandableListAdapter.java

package com.example.androidexpandablelistview;  import java.util.HashMap; import java.util.List;  import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView;  populace course of didactics MyExpandableListAdapter extends BaseExpandableListAdapter {   mortal Context context;  mortal List<String> listGroup;  mortal HashMap<String, List<String>> listChild;   populace MyExpandableListAdapter(Context context, List<String> listGroup,    HashMap<String, List<String>> listChild) {   this.context = context;   this.listGroup = listGroup;   this.listChild = listChild;  }   @Override  populace int getGroupCount() {   supply listGroup.size();  }   @Override  populace int getChildrenCount(int groupPosition) {   supply listChild.get(listGroup.get(groupPosition)).size();  }   @Override  populace Object getGroup(int groupPosition) {   supply listGroup.get(groupPosition);  }   @Override  populace Object getChild(int groupPosition, int childPosition) {   supply listChild.get(listGroup.get(groupPosition)).get(childPosition);  }   @Override  populace long getGroupId(int groupPosition) {   supply groupPosition;  }   @Override  populace long getChildId(int groupPosition, int childPosition) {   supply childPosition;  }   @Override  populace boolean hasStableIds() {   supply false;  }   @Override  populace View getGroupView(int groupPosition, boolean isExpanded,    View convertView, ViewGroup parent) {   if (convertView == null) {    LayoutInflater infalInflater = (LayoutInflater) context      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    convertView = infalInflater.inflate(R.layout.group_layout, null);   }    String textGroup = (String) getGroup(groupPosition);    TextView textViewGroup = (TextView) convertView     .findViewById(R.id.group);   textViewGroup.setText(textGroup);    supply convertView;  }   @Override  populace View getChildView(int groupPosition, int childPosition,    boolean isLastChild, View convertView, ViewGroup parent) {   if (convertView == null) {    LayoutInflater infalInflater = (LayoutInflater) context      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    convertView = infalInflater.inflate(R.layout.item_layout, null);   }    TextView textViewItem = (TextView) convertView.findViewById(R.id.item);    String text = (String) getChild(groupPosition, childPosition);    textViewItem.setText(text);   supply convertView;  }   @Override  populace boolean isChildSelectable(int groupPosition, int childPosition) {   // TODO Auto-generated method stub   supply false;  }  } 

MainActivity.java

package com.example.androidexpandablelistview;  import java.util.ArrayList; import java.util.HashMap; import java.util.List;  import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.widget.ExpandableListView;  populace course of didactics MainActivity extends ActionBarActivity {   ExpandableListView expandableListView;  MyExpandableListAdapter myExpandableListAdapter;  List<String> groupList;  HashMap<String, List<String>> childMap;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);      init();   expandableListView = (ExpandableListView) findViewById(R.id.mylist);   myExpandableListAdapter = novel MyExpandableListAdapter(this, groupList, childMap);   expandableListView.setAdapter(myExpandableListAdapter);  }   mortal void init() {   groupList = novel ArrayList<String>();   childMap = novel HashMap<String, List<String>>();    List<String> groupList0 = novel ArrayList<String>();   groupList0.add("groupList0 - 1");   groupList0.add("groupList0 - 2");   groupList0.add("groupList0 - 3");      List<String> groupList1 = novel ArrayList<String>();   groupList1.add("groupList1 - 1");   groupList1.add("groupList1 - 2");   groupList1.add("groupList1 - 3");    List<String> groupList2 = novel ArrayList<String>();   groupList2.add("groupList2 - 1");   groupList2.add("groupList2 - 2");   groupList2.add("groupList2 - 3");    List<String> groupList3 = novel ArrayList<String>();   groupList3.add("groupList3 - 1");   groupList3.add("groupList3 - 2");   groupList3.add("groupList3 - 3");      groupList.add("Group List 0");   groupList.add("Group List 1");   groupList.add("Group List 2");   groupList.add("Group List 3");    childMap.put(groupList.get(0), groupList0);   childMap.put(groupList.get(1), groupList1);   childMap.put(groupList.get(2), groupList2);   childMap.put(groupList.get(3), groupList3);  }  } 

  populace course of didactics MyExpandableListAdapter extends BaseExpandableListAdapter  ExpandableListView exampleDownload the files.

Next:
Create groupIndicator for ExpandableListView example
– Place groupIndicator of ExpandableListView on correct side