我正在为蜂窝开发一个app,我在操作栏中放了一些标签,但现在我不知道如何将ExpandableListView放在其中一个标签中,或者放在其中一个片段中。
答案 0 :(得分:1)
现在片段不支持ExpandableListView。你必须为此子类化Fragment。如果您不想重新发明轮子,请看一下:https://gist.github.com/1316903
答案 1 :(得分:1)
这对我有用:
片段内部
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
}
,内部课程:
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>