Add a ExpandableListView into a fragment
I am developing an app for honeycomb, and I have put some tabs in the action bar, but now I don't know how to put an ExpandableListView in one of the tabs, 开发者_运维技巧or, in one of the fragments.
ExpandableListView is not supported in fragments for now. You have to subclass Fragment for this. If you don't want to reinvent the wheel, take a look at this: https://gist.github.com/1316903
Here is what is works for me:
Inside of Fragment
private String[] categories;
private String[][] categoryItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categories = getResources().getStringArray(R.array.videos_curriculum);
String[] category1 = getResources().getStringArray(R.array.video_category1);
String[] category2 = getResources().getStringArray(R.array.video_category2);
String[] category3 = getResources().getStringArray(R.array.video_category3);
categoryItems = new String[][]{category1, category2, category3};
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (ExpandableListView) view.findViewById(R.id.expListView);
listView.setAdapter(new VideoGroupsListAdapter(categories, categoryItems));
listView.setGroupIndicator(null); // I don't need group indicator on left
}
, inner class:
public class VideoGroupsListAdapter extends BaseExpandableListAdapter {
private final LayoutInflater inflater;
private String[] groups;
private String[][] children;
public VideoGroupsListAdapter(String[] groups, String[][] children) {
this.groups = groups;
this.children = children;
inflater = LayoutInflater.from(getActivity());
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.new_video_header, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.headerTitleTxt);
holder.icon = (TextView) convertView.findViewById(R.id.headerIconTxt);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getGroup(groupPosition).toString());
if (isExpanded) {
holder.icon.setText(R.string.ic_down);
} else {
holder.icon.setText(R.string.ic_up);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.new_video_list_item, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.titleTxt);
holder.icon = (TextView) convertView.findViewById(R.id.watchedIconTxt);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class ViewHolder {
TextView text;
TextView icon;
}
}
new_videos_header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/ListItem.Header.Light"
android:gravity="center_vertical"
android:paddingLeft="@dimen/default_scr_side_padding"
android:paddingRight="@dimen/default_scr_side_padding"
>
<!--Title-->
<TextView
android:id="@+id/headerTitleTxt"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="8dip"
style="@style/HeaderTitle.Dark"
/>
<!--Right Icon-->
<TextView
android:id="@+id/headerIconTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ic_up"
/>
</LinearLayout>
精彩评论