Skip to content

Instantly share code, notes, and snippets.

@st-f
Last active August 15, 2023 22:52
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save st-f/2b2a838d3f0258c5c33f to your computer and use it in GitHub Desktop.
Save st-f/2b2a838d3f0258c5c33f to your computer and use it in GitHub Desktop.
A 3 level expandable list view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ExpandableListView android:id="@+id/mainList"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_orange_dark"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/eventsListEventRowText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textColor="@android:color/white"
android:textSize="18sp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/eventsListEventRowText"
android:paddingLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="14sp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:background="@android:color/holo_green_dark">
<TextView
android:id="@+id/eventsListEventRowText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="24sp"/>
</LinearLayout>
package net.stf.threelevelexpandablelistview;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
/**
* New and improved. Feel free to contact me if you have any questions.
*/
public class ThreeLevelExpandableListView extends AppCompatActivity {
public static final int FIRST_LEVEL_COUNT = 6;
public static final int SECOND_LEVEL_COUNT = 4;
public static final int THIRD_LEVEL_COUNT = 20;
private ExpandableListView expandableListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.mainList);
expandableListView.setAdapter(new ParentLevel(this));
}
public class ParentLevel extends BaseExpandableListAdapter {
private Context context;
public ParentLevel(Context context) {
this.context = context;
}
@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) {
SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(MainActivity.this);
secondLevelELV.setAdapter(new SecondLevelAdapter(context));
secondLevelELV.setGroupIndicator(null);
return secondLevelELV;
}
@Override
public int getChildrenCount(int groupPosition) {
return SECOND_LEVEL_COUNT;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return FIRST_LEVEL_COUNT;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText("FIRST LEVEL");
}
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class SecondLevelExpandableListView extends ExpandableListView {
public SecondLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;
public SecondLevelAdapter(Context context) {
this.context = context;
}
@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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_third, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText("SECOND LEVEL");
}
return convertView;
}
@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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_second, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText("THIRD LEVEL");
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return THIRD_LEVEL_COUNT;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
@tiwarikk
Copy link

how to click event on 3rd level child please check sir
Thanks i am waiting your response

@tabetuba
Copy link

It means u are a master developer in android
thanks for your tutorial

@vikasrajput007
Copy link

its a hundreds dollar code

@bereket-kassahun
Copy link

nice code, thanks.

@muhammadumair4533
Copy link

can yuo help ma to three level expandable list view inside drawer layout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment