Last active
December 6, 2020 23:52
-
-
Save bowmanb/4052030 to your computer and use it in GitHub Desktop.
A basic Android ExpandableListFragment (SavedTabsFragment) example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thankyou