<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:l" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:l" />
Activity_main.xml
=====================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f4f4f4"
tools:context="com.sam.web.expandablelistview.MainActivity">
<ExpandableListView
android:id="@+id/expandableListViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
</LinearLayout>
==========================================================================
group_layout.xml
=================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="8dp"
android:background="#ffbb00"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:paddingLeft="?android:expandableListPreferredItemPaddingLeft"
android:textSize="17sp"
android:id="@+id/headerTextViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
============================================================================
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:textColor="#000000"
android:id="@+id/childTextViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
===========================================================================
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;
@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;
}
});
}
private void prepareListData() {
String[] headerString=getResources().getStringArray(R.array.abbreviations_list_header);
String[] childString=getResources().getStringArray(R.array.abbreviations_list_child);
listDataHeader=new ArrayList<>();
listDataChild=new HashMap<>();
for (int i=0; i<headerString.length; i++){
// adding header data
listDataHeader.add(headerString[i]);
List<String> child=new ArrayList<>();
child.add(childString[i]);
listDataChild.put(listDataHeader.get(i),child);
}
}
}
======================================================================================================
CustomAdapter.java
====================
package com.sam.web.expandablelistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
/**
* Created by maudud on 1/22/18.
*/
public class CustomAdapter extends BaseExpandableListAdapter {
private Context context;
List<String> listDataHeader;
HashMap<String,List<String>> listDataChild;
public CustomAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listDataChild;
}
@Override
public int getGroupCount() {
return listDataHeader.size();
}
@Override
public int getChildrenCount(int i) {
return listDataChild.get(listDataHeader.get(i)).size();
}
@Override
public Object getGroup(int i) {
return listDataHeader.get(i);
}
@Override
public Object getChild(int i, int i1) {
return listDataChild.get(listDataHeader.get(i)).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String headerText=(String) getGroup(i);
if(view==null){
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.group_layout,null);
}
TextView textView=view.findViewById(R.id.headerTextViewId);
textView.setText(headerText);
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
String childText=(String) getChild(i,i1);
if(view==null){
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.child_layout,null);
}
TextView textView=view.findViewById(R.id.childTextViewId);
textView.setText(childText);
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
============================================================================================================================
Strings.xml
=============
<resources>
<string name="app_name">ExpandableListView</string>
<string-array name="abbreviations_list_header">
<item>AAI</item>
<item>ALu</item>
<item>ATI</item>
<item>AVI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
<item>AAI</item>
</string-array>
<string-array name="abbreviations_list_child">
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
<item>Advanced audio coding
AAC is coding as an audio coding which is able to coding </item>
</string-array>
</resources>