1

我正在开发一个应用程序,其中两个片段在一个活动中。

一个是ExpandableListViewFragment,另一个是对应的布局片段。

第一个片段将显示expandableListItem单击任何组或子项的任何项目。然后与该项目 ( ) 对应的布局expandableListItem将在其他片段中膨胀。我做了这些使用ListView但不是在ExpandableListView.

请帮助我。提前致谢。

PK

4

1 回答 1

0

我想你需要的只是:

ExpandableListView,一个扩展 BaseExpandableListAdapter 的适配器,以及 groupView 和子视图的相应布局。

扩展 BaseExpandableListAdapter 会迫使您实现一些方法,但是一旦您考虑它们,它并不是很难理解。

诀窍都在适配器内部。您可以为父级和子级扩展布局。看看我的例子:

这样,您的父视图会随着父布局而膨胀,而子视图也会随着它们的相应布局而膨胀。希望能帮助到你。

public class ExpadableAdapter extends BaseExpandableListAdapter {
Activity context;

List<Movie> listObjects;

private static LayoutInflater inflaterParent = null;
private static LayoutInflater inflaterChild = null;

private ImageLoader imageLoader; 
//on the constructor pass on the list of objects
public ExpadableAdapter(Activity context, List<Object> listObjects) {
    context = context;
    this.listObjects = listObjects; 
    inflaterParent = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflaterChild = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public Object getChild(int groupPosition, int childPosition) {
    //in this case I fetch the children on a DB, you can as well have a two dimensions array at first
    List<Day> parentChildren = DBUtils.getObjectChildren(context, listObjects.get(groupPosition).getId());
    return parentChildren.get(childPosition);
}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    Object object = listObjects.get(groupPosition);
    //the first inflation for children views
    View vi = convertView;
    if(convertView==null)
        vi = inflaterChild.inflate(R.layout.child_layout, null);

    List<Day> listChildren = DBUtils.getChildren(context, listObjects.get(groupPosition).getId());

    TextView textView = (TextView) vi.findViewById(R.id.child_layout_textview);
    return vi;
}

public int getChildrenCount(int groupPosition) {
    return listObjects.get(groupPosition).getCount();
}

public Object getGroup(int groupPosition) {
    return listObjects.get(groupPosition);
}

public int getGroupCount() {
    return listObjects.size();
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    View vi=convertView;
    //the parent inflater.
    if(convertView==null)
        vi = inflaterParent.inflate(R.layout.parent_layout, null);

    TextView textView=(TextView)vi.findViewById(R.id.parent_layout_text_view);;
    textView.setText(listObjects.get(groupPosition).getName());

    return vi;
}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}
于 2012-06-13T10:58:15.067 回答