Skip to content

Instantly share code, notes, and snippets.

@kevalpatel2106
Last active February 17, 2017 17:44
Show Gist options
  • Save kevalpatel2106/57ee5455c4d5db7c05c2e2a54b687a39 to your computer and use it in GitHub Desktop.
Save kevalpatel2106/57ee5455c4d5db7c05c2e2a54b687a39 to your computer and use it in GitHub Desktop.
Infinite level expandable listview in android. Visit full project : https://github.com/kevalpatel2106/android-samples/tree/master/InfiniteExpandableListView
<?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"
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="com.infiniteexpandablelistview.MainActivity">
<ExpandableListView
android:id="@+id/ParentLevel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="@null" />
</RelativeLayout>
package com.infiniteexpandablelistview;
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.ArrayList;
/**
* Created by Keval on 16-Feb-17.
*
* @author {@link 'https://github.com/kevalpatel2106'}
*/
public class InfiniteListAdapter extends BaseExpandableListAdapter {
private final Context mContext;
private final ArrayList<Subjects> mSubjects;
public InfiniteListAdapter(Context context, ArrayList<Subjects> subjects) {
mContext = context;
mSubjects = subjects;
}
@Override
public int getGroupCount() {
return mSubjects.size();
}
@Override
public int getChildrenCount(int groupItemPos) {
return mSubjects.get(groupItemPos).getSubList() == null
|| mSubjects.get(groupItemPos).getSubList().size() == 0 ? 0 : 1;
}
@Override
public Subjects getGroup(int groupItemPos) {
return mSubjects.get(groupItemPos);
}
@Override
public Subjects getChild(int groupItemPos, int childItemPos) {
return mSubjects.get(groupItemPos).getSubList().get(childItemPos);
}
@Override
public long getGroupId(int groupItemPos) {
return groupItemPos;
}
@Override
public long getChildId(int groupItemPos, int childItemPos) {
return childItemPos;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupItemPos, boolean isExpanded, View convertView, ViewGroup viewGroup) {
GroupViewHolder groupViewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_group, viewGroup, false);
groupViewHolder = new GroupViewHolder();
groupViewHolder.mTextView = (TextView) convertView.findViewById(R.id.row_subject_title);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
Subjects parentItem = getGroup(groupItemPos);
if (parentItem != null) {
groupViewHolder.mTextView.setText(parentItem.getTitle());
}
return convertView;
}
@Override
public View getChildView(int groupPos, int childPos, boolean isExpanded, View convertView, ViewGroup viewGroup) {
ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_child, viewGroup, false);
childViewHolder = new ChildViewHolder();
childViewHolder.mExpandableListView = (SecondLevelExpandableListView) convertView.findViewById(R.id.row_group_list);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
Subjects childItem = getChild(groupPos, childPos);
if (childItem != null) {
childViewHolder.mExpandableListView.setAdapter(new InfiniteListAdapter(mContext, getGroup(groupPos).getSubList()));
}
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
private class GroupViewHolder {
private TextView mTextView;
}
private class ChildViewHolder {
private SecondLevelExpandableListView mExpandableListView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.infiniteexpandablelistview.SecondLevelExpandableListView
android:id="@+id/row_group_list"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/row_subject_title"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="16sp"
android:orientation="vertical"/>
package com.infiniteexpandablelistview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ExpandableListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Subjects> mExampleList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExampleList = generateList(1);
ExpandableListView mainListView = (ExpandableListView) findViewById(R.id.ParentLevel);
mainListView.setAdapter(new InfiniteListAdapter(this, mExampleList));
}
private ArrayList<Subjects> generateList(int level) {
ArrayList<Subjects> subjectses = new ArrayList<>();
for (int i = 0; i < 6; i++) {
Subjects subjects = new Subjects();
subjects.setTitle("Level " + level + " -(" + i + ")");
subjects.setId(i);
if (level < 5)
subjects.addSubjects(generateList(level + 1));
subjectses.add(subjects);
}
return subjectses;
}
}
package com.infiniteexpandablelistview;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.ExpandableListView;
/**
* Created by Keval on 16-Feb-17.
*
* @author {@link 'https://github.com/kevalpatel2106'}
*/
public class SecondLevelExpandableListView extends ExpandableListView {
public SecondLevelExpandableListView(Context context) {
super(context);
}
public SecondLevelExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SecondLevelExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public SecondLevelExpandableListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
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);
}
}
package com.infiniteexpandablelistview;
import java.util.ArrayList;
/**
* Created by Keval on 16-Feb-17.
*
* @author {@link 'https://github.com/kevalpatel2106'}
*/
public class Subjects {
private ArrayList<Subjects> subList = new ArrayList<>();//second level sublist
public Subjects(int id, String title, int sort, boolean sub) {
this.id = id;
this.title = title;
this.sort = sort;
this.hasSub = sub;
}
public Subjects(int id, String title) {
this.id = id;
this.title = title;
}
private int id;
public Subjects() {
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isHasSub() {
return hasSub;
}
public void setHasSub(boolean hasSub) {
this.hasSub = hasSub;
}
private int sort;
private String title;
private boolean hasSub;
//second level sublist
public ArrayList<Subjects> getSubList() {
return subList;
}
// add second level sublist
public void addSubject(Subjects subject) {
this.subList.add(subject);
}
// add second level sublist
public void addSubjects(ArrayList<Subjects> subject) {
this.subList = (subject);
}
}
@kevalpatel2106
Copy link
Author

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