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