Skip to content

Instantly share code, notes, and snippets.

@bowmanb
Last active December 6, 2020 23:52
Show Gist options
  • Save bowmanb/4052030 to your computer and use it in GitHub Desktop.
Save bowmanb/4052030 to your computer and use it in GitHub Desktop.
A basic Android ExpandableListFragment (SavedTabsFragment) example.
<?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:id="@+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
package com.advinture.ukuleletabs.fragments;
import android.app.ExpandableListActivity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.advinture.ukuleletabs.R;
/**
* Pieced together from:
* Android samples: com.example.android.apis.view.ExpandableList1
* http://androidword.blogspot.com/2012/01/how-to-use-expandablelistview.html
* http://stackoverflow.com/questions/6938560/android-fragments-setcontentview-alternative
* http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment-android
*/
public class SavedTabsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.saved_tabs, null);
ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.list);
elv.setAdapter(new SavedTabsListAdapter());
return v;
}
public class SavedTabsListAdapter extends BaseExpandableListAdapter {
private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int i) {
return children[i].length;
}
@Override
public Object getGroup(int i) {
return groups[i];
}
@Override
public Object getChild(int i, int i1) {
return children[i][i1];
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
TextView textView = new TextView(SavedTabsFragment.this.getActivity());
textView.setText(getGroup(i).toString());
return textView;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
TextView textView = new TextView(SavedTabsFragment.this.getActivity());
textView.setText(getChild(i, i1).toString());
return textView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
}
@JacobCho
Copy link

Thanks a lot for this. How would you go about styling it? The group headers in particular

@bowmanb
Copy link
Author

bowmanb commented Dec 4, 2013

@Racepace7 Override getGroupView and inflate a custom layout.

@M-Tumas
Copy link

M-Tumas commented May 10, 2014

HI, can u get some info about styling list? add button and etc. Thanks a lot!

@SerirWalids
Copy link

Thanks a lot for this :) ,

@khchoi
Copy link

khchoi commented Aug 11, 2014

Thanks!! :)

@manishhsgk
Copy link

Thanks a lot... :)

@SKsutar
Copy link

SKsutar commented Apr 13, 2015

thanks a lot

@albersmc
Copy link

Save my day, thanks a lot!!!

@kamanzi75
Copy link

thanks alot,,,,,,,,,,,,,,

@SathishMalage
Copy link

Please help me ..i'm getting "classcast exception"

@sujeesh
Copy link

sujeesh commented Aug 26, 2015

how to set image to the above ex_listview???

@sajith-udurawana
Copy link

Saved me. Thanks alot!

@Ghostsniper13
Copy link

Still wondering how do you format the style and text for this very handy expandable list.

Or how to use an inflater from another xml files. Thanks in advance.

@sajith-udurawana
Copy link

you can pass the context in the constructor for SavedTabsListAdapter and then inflate the custom xml layout for group and child views.

@ussenuk
Copy link

ussenuk commented Aug 29, 2016

thank u. you have saved my design, i was stack for so a long period of time before founding this code.

@diwakarvishwas
Copy link

Sir , how can i use OnClickListners on the sub items of list to open a new activity.

@phillipcutter
Copy link

This is awesome and super concise, thanks you so much!

@adityasonel
Copy link

Please give an example for how to inflate custom layout for group header and items ?

@GKhan2018
Copy link

Thanks. This is working

@madhulikavecha
Copy link

thanks a lott....its working very fine

@nikita2811
Copy link

thankyou

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