Skip to content

Instantly share code, notes, and snippets.

@bowmanb
Last active December 6, 2020 23:52
Show Gist options
  • Select an option

  • Save bowmanb/4052030 to your computer and use it in GitHub Desktop.

Select an option

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;
}
}
}
@arman2123

Copy link
Copy Markdown

Awesome post. Thanks a bunch :D

@bowmanb

bowmanb commented Jul 20, 2013

Copy link
Copy Markdown
Author

No problem, glad you found it useful!

@mclhrn

mclhrn commented Jul 29, 2013

Copy link
Copy Markdown

Brilliant! Thanks.

@Mmaaikel

Copy link
Copy Markdown

Thanks! Needed this!

@JacobCho

Copy link
Copy Markdown

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

@bowmanb

bowmanb commented Dec 4, 2013

Copy link
Copy Markdown
Author

@Racepace7 Override getGroupView and inflate a custom layout.

@M-Tumas

M-Tumas commented May 10, 2014

Copy link
Copy Markdown

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

@SerirWalids

Copy link
Copy Markdown

Thanks a lot for this :) ,

@khchoi

khchoi commented Aug 11, 2014

Copy link
Copy Markdown

Thanks!! :)

@manishhsgk

Copy link
Copy Markdown

Thanks a lot... :)

@SKsutar

SKsutar commented Apr 13, 2015

Copy link
Copy Markdown

thanks a lot

@albersmc

Copy link
Copy Markdown

Save my day, thanks a lot!!!

@kamanzi75

Copy link
Copy Markdown

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

@SathishMalage

Copy link
Copy Markdown

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

@sujeesh

sujeesh commented Aug 26, 2015

Copy link
Copy Markdown

how to set image to the above ex_listview???

@sajith-udurawana

Copy link
Copy Markdown

Saved me. Thanks alot!

@Ghostsniper13

Copy link
Copy Markdown

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
Copy Markdown

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

@ussenuk

ussenuk commented Aug 29, 2016

Copy link
Copy Markdown

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

@diwakarvishwas

Copy link
Copy Markdown

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

@phillipcutter

Copy link
Copy Markdown

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

@adityasonel

Copy link
Copy Markdown

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

@GKhan2018

Copy link
Copy Markdown

Thanks. This is working

@madhulikavecha

Copy link
Copy Markdown

thanks a lott....its working very fine

@nikita2811

Copy link
Copy Markdown

thankyou

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