Friday, September 16, 2011

Three Level Expandable list

The below snippet helps to implement three level list in android.
First Group
--Sub Group
   ---   Child1
   ---   Child 2
   ---   Child 3
in this pattern.....
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
    <ExpandableListView
    android:layout_width="fill_parent"
    android:id="@+id/ParentLevel"
    android:groupIndicator="@null"
    android:layout_height="fill_parent">
    </ExpandableListView>

</LinearLayout>

Home.java
package com.threeelevellist.activities;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class Home extends Activity 
{
    ExpandableListView explvlist;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
        explvlist.setAdapter(new ParentLevel());
        
    }
    
    public class ParentLevel extends BaseExpandableListAdapter
    {

@Override
public Object getChild(int arg0, int arg1) 
{
return arg1;
}

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

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) 
{
CustExpListview SecondLevelexplv = new CustExpListview(Home.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}

@Override
public int getChildrenCount(int groupPosition) 
{
return 3;
}

@Override
public Object getGroup(int groupPosition) 
{
return groupPosition;
}

@Override
public int getGroupCount() 
{
return 5;
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("->FirstLevel");
tv.setBackgroundColor(Color.BLUE);
tv.setPadding(10, 7, 7, 7);
return tv;
}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) 
{
return true;
}    
    }
    
    public class CustExpListview extends ExpandableListView
    {
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context) 
{
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
    }
    
    public class SecondLevelAdapter extends BaseExpandableListAdapter
    {

@Override
public Object getChild(int groupPosition, int childPosition) 
{
return childPosition;
}

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

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("child");
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.YELLOW);
tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return tv;
}

@Override
public int getChildrenCount(int groupPosition) 
{
return 5;
}

@Override
public Object getGroup(int groupPosition) 
{
return groupPosition;
}

@Override
public int getGroupCount() 
{
return 1;
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("-->Second Level");
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.RED);
return tv;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
   
    }
}

7 comments:

  1. Nice thanks, i'm gonna try this now, but after i ran it i got a feeling my search is over ;)

    ReplyDelete
  2. thanks its really helpful. but i need one help i want to expand only one subgroup at a time can u please help me. i m stuck... please help

    ReplyDelete
  3. How do I change the names of each level so They all will be unique?

    Thanks

    ReplyDelete
  4. how to one more level for this means fourth level

    ReplyDelete
  5. hi nice post , by using this code i've implemented four levels but some space problem coming, how to reolve that problem

    ReplyDelete
  6. How to add different children to different parents

    ReplyDelete
  7. Thanks this tutoriall is very useful.

    ReplyDelete