Android, ExpandableListView is a View that shows items in a vertically scrolling two level list. Different from the listview by allowing two level groups which can individually be expanded to show.Expandable listview is a parent and childview to expand to click parent view to display childview in android.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bhaumik.expandablelistwithactivity.MainActivity"
android:background="@color/MainActivityBackground">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/expand_view" />
</RelativeLayout>
Create XML File to Parent Item Display in Layout
PrentItem.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="match_parent"
android:background="@drawable/parent_item_bg">
<TextView
android:id="@+id/tv_parent_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/WhiteColor"
android:text="Parent Item"
android:textSize="20dp"
android:padding="5dp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
Create XML Layout file to Child Item in Listview
ChildItem.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="match_parent"
android:background="@drawable/child_item_bg">
<TextView
android:id="@+id/tv_child_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/BlackColor"
android:text="Child Item"
android:textSize="20dp"
android:padding="8dp"
android:layout_marginLeft="80dp"/>
</LinearLayout>
MainActivity.java
package com.bhaumik.expandablelistwithactivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.transition.Explode;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.jar.Manifest;
public class MainActivity extends FragmentActivity {
ExpandableListView expandableListView;
List<String> main_title = new ArrayList<>();
List<String> android_list = new ArrayList<>();
List<String> java_list = new ArrayList<>();
List<String> php_list = new ArrayList<>();
HashMap<String,List<String>> child_list = new HashMap<String,List<String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.expand_view);
String[] cource_title = getResources().getStringArray(R.array.cource_title);
String[] android_items = getResources().getStringArray(R.array.android_item);
String[] java_items = getResources().getStringArray(R.array.java_item);
String[] php_items = getResources().getStringArray(R.array.php_item);
for(String heading : cource_title)
{
main_title.add(heading);
}
for (String android : android_items)
{
android_list.add(android);
}
for (String java : java_items)
{
java_list.add(java);
}
for (String php : php_items)
{
php_list.add(php);
}
child_list.put(main_title.get(0),android_list);
child_list.put(main_title.get(1),java_list);
child_list.put(main_title.get(2),php_list);
final MyAdapter myAdapter = new MyAdapter(MainActivity.this,main_title,child_list);
expandableListView.setAdapter(myAdapter);
}
}
Create Adapter to set Parent And Child item Data in listview.
MyAdapter.java
package com.bhaumik.expandablelistwithactivity;
import android.content.Context;
import android.content.Intent;
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 Bhaumik on 3/23/2017.
*/
public class MyAdapter extends BaseExpandableListAdapter{
Context context;
List<String> cource_title;
HashMap<String,List<String>> child_title;
public MyAdapter(Context context, List<String> cource_title, HashMap<String, List<String>> child_title) {
this.context = context;
this.cource_title = cource_title;
this.child_title = child_title;
}
@Override
public int getGroupCount() {
return cource_title.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return child_title.get(cource_title.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return cource_title.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child_title.get(cource_title.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String titles = (String) this.getGroup(groupPosition);
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.parent_item,null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tv_parent_item);
textView.setText(titles);
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String titles = (String) this.getChild(groupPosition,childPosition);
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child_item,null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tv_child_item);
textView.setText(titles);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,DetailActivity.class);
intent.putExtra("text",child_title.get(cource_title.get(groupPosition)).get(childPosition));
context.startActivity(intent);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}