MainActivity.java
=======================
package com.sam.web.expandablelistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
private CustomAdapter customAdapter;
List<String> listDataHeader;
HashMap<String,List<String>>listDataChild;
private int lastExpandablePosition=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareListData();
expandableListView=(ExpandableListView) findViewById(R.id.expandableListViewId);
customAdapter=new CustomAdapter(this,listDataHeader,listDataChild);
expandableListView.setAdapter(customAdapter);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int i, long id) {
String groupName=listDataHeader.get(i);
Toast.makeText(getApplicationContext(),"group Name :"+groupName,Toast.LENGTH_SHORT).show();
return false;
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int i) {
String groupName=listDataHeader.get(i);
Toast.makeText(getApplicationContext(),groupName+"is Collapsed",Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int i, int il, long id) {
String childString=listDataChild.get(listDataHeader.get(i)).get(il);
Toast.makeText(getApplicationContext(),childString,Toast.LENGTH_SHORT).show();
return false;
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int i) {
if(lastExpandablePosition!=-1 && lastExpandablePosition!=i){
expandableListView.collapseGroup(lastExpandablePosition);
}
lastExpandablePosition=i;
}
});
}
private void prepareListData() {
listDataHeader=new ArrayList<>();
listDataChild=new HashMap<>();
listDataHeader.add("1. Overview");
listDataHeader.add("2. ProgramStucture");
listDataHeader.add("3. Keyword");
listDataHeader.add("4. Variable");
listDataHeader.add("5. Operators");
listDataHeader.add("6. Comments");
/* listDataHeader.add("7. Math h");
listDataHeader.add("8. Datatype");
listDataHeader.add("9. Keyword");
listDataHeader.add("10. Program Structure");
listDataHeader.add("11. Control Statement");
listDataHeader.add("12. Array");
listDataHeader.add("13. Print");
listDataHeader.add("14. Function");
listDataHeader.add("15. Pointer");
listDataHeader.add("16. Stucture");
listDataHeader.add("17. Files");*/
List<String> overView=new ArrayList<>();
overView.add("1.1 What is c language");
overView.add("1.2 History of c ");
overView.add("1.3 Feature of c");
overView.add("1.4 Advantages of c");
List<String> programStucture=new ArrayList<>();
programStucture.add("1.1 What is c language");
programStucture.add("1.2 History of c ");
programStucture.add("1.3 Feature of c");
programStucture.add("1.4 Advantages of c");
List<String> keyword=new ArrayList<>();
keyword.add("1.1 What is c language");
keyword.add("1.2 History of c ");
keyword.add("1.3 Feature of c");
keyword.add("1.4 Advantages of c");
List<String> variable=new ArrayList<>();
variable.add("1.1 What is c language");
variable.add("1.2 History of c ");
variable.add("1.3 Feature of c");
variable.add("1.4 Advantages of c");
List<String> operators=new ArrayList<>();
operators.add("1.1 What is c language");
operators.add("1.2 History of c ");
operators.add("1.3 Feature of c");
operators.add("1.4 Advantages of c");
List<String> comments=new ArrayList<>();
comments.add("1.1 What is c language");
comments.add("1.2 History of c ");
comments.add("1.3 Feature of c");
comments.add("1.4 Advantages of c");
listDataChild.put(listDataHeader.get(0),overView);
listDataChild.put(listDataHeader.get(1),programStucture);
listDataChild.put(listDataHeader.get(2),keyword);
listDataChild.put(listDataHeader.get(3),variable);
listDataChild.put(listDataHeader.get(4),operators);
listDataChild.put(listDataHeader.get(5),comments);
}
}
==============================================================================================
child_layout.xml
===============
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="55dp">
<TextView
android:paddingLeft="?android:expandableListPreferredChildPaddingLeft"
android:textSize="17sp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textColor="#000000"
android:id="@+id/childTextViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>