Expandable Listview

Introduction
   Expandable list view is used to group list data by categories. It has the capability of expanding and collapsing the groups when user touches header. n this example creating a custom Expandable ListView with parent and child rows. 
demo


Using this code

activity_main.xml:
<?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="fill_parent"
android:orientation="vertical"
android:background="#A4A4A4" >

<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="@null"
android:groupIndicator="@null"
android:dividerHeight="0.5dp"
android:cacheColorHint="#00000000"/>

</LinearLayout>

list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="#0000FF"
android:orientation="vertical">

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:background="#0080FF"
android:layout_height="55dp"
android:padding="3dp">

<TextView
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textColor="#f9f93d"
android:textSize="17dp" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/side" />
</RelativeLayout>
</RelativeLayout>

list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#CCFF99"
android:orientation="vertical" >

<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:textColor="@drawable/select_child"
android:textSize="17sp" />

</RelativeLayout>

MainActivity.java
package com.kalisandroid.expandablelistview;

import info.androidhive.expandablelistview.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
boolean ischecked;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);

// preparing list data
prepareListData();

listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

// setting list adapter
expListView.setAdapter(listAdapter);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
int previousGroup = -1;

@Override
public void onGroupExpand(int groupPosition)
{
if(groupPosition != previousGroup)
expListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});

// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
TextView a;
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),listDataChild.
get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show();
return false;
}
});
}

/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

// Adding header data
listDataHeader.add("India");
listDataHeader.add("Pakistan");
listDataHeader.add("Srilanka");
listDataHeader.add("Chinna");

// Adding child data
List<String> India = new ArrayList<String>();
India.add("Chennai");
India.add("New Delhi");
India.add("Mumbai");
India.add("Kolkata");
India.add("Bangalore");
India.add("Hyderabad");

// Adding child data
List<String> Pakistan = new ArrayList<String>();
Pakistan.add("Karachi");
Pakistan.add("Lahore");
Pakistan.add("Faisalabad");
Pakistan.add("Rawalpindi");

// Adding child data
List<String> Srilanka = new ArrayList<String>();
Srilanka.add("Colombo");
Srilanka.add("Jaffna");
Srilanka.add("Kandy");
Srilanka.add("Negombo");
Srilanka.add("Talawakele");

// Adding child data
List<String> Chinna = new ArrayList<String>();
Chinna.add("Beijing");
Chinna.add("Shanghai");
Chinna.add("Chongqing");
Chinna.add("Hong Kong");
Chinna.add("Guangzhou");

listDataChild.put(listDataHeader.get(0), India); // Header, Child data
listDataChild.put(listDataHeader.get(1), Pakistan);
listDataChild.put(listDataHeader.get(2), Srilanka);
listDataChild.put(listDataHeader.get(3), Chinna);
}
}

ExpandableListAdapter.java
package com.kalisandroid.expandablelistview;

import info.androidhive.expandablelistview.R;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
private HashMap<String, List<String>> _listDataChild;
boolean ischecked;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon)
{
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

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

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);

txtListChild.setText(childText);

return convertView;
}

@Override
public int getChildrenCount(int groupPosition)
{
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).
size();
}

@Override
public Object getGroup(int groupPosition)
{
return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount()
{
return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
ImageView img=(ImageView)convertView.findViewById(R.id.imageView1);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);

if(isExpanded)
{
img.setImageResource(R.drawable.bottom);
}
if(!isExpanded)
{
img.setImageResource(R.drawable.side);
}

return convertView;
}

@Override
public boolean hasStableIds()
{
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}

                                                             
Previous
Next Post »

1 comments:

Write comments
Unknown
AUTHOR
October 14, 2015 at 4:33 AM delete

Hi, what if I want to inflate different layouts by clicking on different child views?

Reply
avatar