I want to add one more level of in ExpandableListView.In current ExpandableListView is two level how can i add one more level. I am new in android developing please help me.
activity_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>
MainActivity.Java
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;
}
}
}
You can try my following sample code. I have posted my full project to GitHub
Of course, you should modify more to meet all your requirements. For basic case, I only use the data source in the arrays.xml
file. Hope this helps!
arrays.xml:
<resources>
<string-array name="items_array_expandable_level_one">
<item>Level 1.1</item>
<item>Level 1.2</item>
<item>Level 1.3</item>
</string-array>
<string-array name="items_array_expandable_level_one_one_child">
<item>Level 1.1.1</item>
<item>Level 1.1.2</item>
</string-array>
<string-array name="items_array_expandable_level_one_two_child">
<item>Level 1.2.1</item>
</string-array>
<string-array name="items_array_expandable_other_child">
<item>Second Level 01</item>
<item>Second Level 02</item>
<item>Second Level 03</item>
</string-array>
<string-array name="items_array_expandable_level_three">
<item>Child Level 01</item>
<item>Child Level 02</item>
</string-array>
</resources>
CustomExpListView.java:
public class CustomExpListView extends ExpandableListView
{
public CustomExpListView(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);
}
}
ParentLevelAdapter.java:
public class ParentLevelAdapter extends BaseExpandableListAdapter {
private final Context mContext;
private final List<String> mListDataHeader;
private final Map<String, List<String>> mListData_SecondLevel_Map;
private final Map<String, List<String>> mListData_ThirdLevel_Map;
public ParentLevelAdapter(Context mContext, List<String> mListDataHeader) {
this.mContext = mContext;
this.mListDataHeader = new ArrayList<>();
this.mListDataHeader.addAll(mListDataHeader);
// Init second level data
String[] mItemHeaders;
mListData_SecondLevel_Map = new HashMap<>();
int parentCount = mListDataHeader.size();
for (int i = 0; i < parentCount; i++) {
String content = mListDataHeader.get(i);
switch (content) {
case "Level 1.1":
mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_one_child);
break;
case "Level 1.2":
mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_two_child);
break;
default:
mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_other_child);
}
mListData_SecondLevel_Map.put(mListDataHeader.get(i), Arrays.asList(mItemHeaders));
}
// THIRD LEVEL
String[] mItemChildOfChild;
List<String> listChild;
mListData_ThirdLevel_Map = new HashMap<>();
for (Object o : mListData_SecondLevel_Map.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Object object = entry.getValue();
if (object instanceof List) {
List<String> stringList = new ArrayList<>();
Collections.addAll(stringList, (String[]) ((List) object).toArray());
for (int i = 0; i < stringList.size(); i++) {
mItemChildOfChild = mContext.getResources().getStringArray(R.array.items_array_expandable_level_three);
listChild = Arrays.asList(mItemChildOfChild);
mListData_ThirdLevel_Map.put(stringList.get(i), listChild);
}
}
}
}
@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) {
final CustomExpListView secondLevelExpListView = new CustomExpListView(this.mContext);
String parentNode = (String) getGroup(groupPosition);
secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, mListData_SecondLevel_Map.get(parentNode), mListData_ThirdLevel_Map));
secondLevelExpListView.setGroupIndicator(null);
return secondLevelExpListView;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.mListDataHeader.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 layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.drawer_list_group, parent, false);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setTextColor(Color.CYAN);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
} }
SecondLevelAdapter.java:
public class SecondLevelAdapter extends BaseExpandableListAdapter
{
private final Context mContext;
private final List<String> mListDataHeader;
private final Map<String, List<String>> mListDataChild;
public SecondLevelAdapter(Context mContext, List<String> mListDataHeader, Map<String, List<String>> mListDataChild) {
this.mContext = mContext;
this.mListDataHeader = mListDataHeader;
this.mListDataChild = mListDataChild;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(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)
{
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.drawer_list_item, parent, false);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition)
{
try {
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size();
} catch (Exception e) {
return 0;
}
}
@Override
public Object getGroup(int groupPosition)
{
return this.mListDataHeader.get(groupPosition);
}
@Override
public int getGroupCount()
{
return this.mListDataHeader.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 layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.drawer_list_group_second, parent, false);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setText(headerTitle);
lblListHeader.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
lblListHeader.setTextColor(Color.YELLOW);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
// Init top level data
List<String> listDataHeader = new ArrayList<>();
String[] mItemHeaders = getResources().getStringArray(R.array.items_array_expandable_level_one);
Collections.addAll(listDataHeader, mItemHeaders);
ExpandableListView mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
if (mExpandableListView != null) {
ParentLevelAdapter parentLevelAdapter = new ParentLevelAdapter(this, listDataHeader);
mExpandableListView.setAdapter(parentLevelAdapter);
}
}
}
Screenshot result:
In this case you should create your own Expandable class.
Refer this link. http://androidcodesnips.blogspot.in/2011/09/three-level-expandable-list.html
Create one more expandable list adapter extending BaseExpandableListAdapter, and in your ExpandableListAdapter getChildView(), call the new expandable list adapter.
I have implemented the same using following link: https://github.com/talhahasanzia/Three-Level-Expandable-Listview