如何将页脚视图添加到ExpandableListActivity中的子列表?

时间:2010-12-27 22:57:08

标签: android listview expandablelistview

我一直试图找出如何在View中为孩子List添加ExpandableListActivity,但无济于事。

我可以获取ExpandableListView并在其上调用addFooterView(),但这只会将新的View添加到群组列表中。 (数据来自Cursor)。

感激地收到任何建议。

干杯

詹姆斯

3 个答案:

答案 0 :(得分:14)

不幸的是,没有ExpandableListView.addChildFooterView()方法,甚至ExpandableListView.addFooterView()。您调用的方法是继承的ListView.addFooterView(),它只是将视图添加为整个列表中的最后一项。它没有被团体/儿童行为所覆盖。

最简单的方法可能只是在ExpandableListAdapter实现中构建一些内容,以便getChildrenCount()返回数据集大小+ 1,然后返回getChildView()中的页脚视图为适当的位置。此外,getChildView()提供了一个布尔参数来标记何时检索任何特定组中的最后一个子项。

答案 1 :(得分:2)

我已经制作了这样的自定义适配器,然后将其实例传递给我的ExpandableListView

public class MyExpandableAdapter extends BaseExpandableListAdapter {
Context context;
List<ParentObject> parentObjects;

public MyExpandableAdapter(Context context, List<ParentObject> parentObjects)
{
    this.context = context;
    this.parentObjects = parentObjects;
}
@Override
public int getGroupCount() {
    return parentObjects.size();
}

 //Add 2 to childcount. The first row and the last row are used as header and footer to childview
@Override
public int getChildrenCount(int i) {
    return parentObjects.get(i).childObjects.size() +2;
}

@Override
public ParentObject getGroup(int i) {
    return parentObjects.get(i);
}

@Override
public ChildObject getChild(int i, int i2) {
    return parentObjects.get(i).childObjects.get(i2);
}

@Override
public long getGroupId(int i) {
    return i;
}

@Override
public long getChildId(int i, int i2) {
    return 0;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
    ParentObject currentParent = parentObjects.get(i);
    if(view ==null)
    {
        LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.parent_row,null);
    }
    TextView txtMother = (TextView)view.findViewById(R.id.txtMother);
    TextView txtFather = (TextView)view.findViewById(R.id.txtFather);
    txtMother.setText(currentParent.mother);
    txtFather.setText(currentParent.father);
    return view;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    ParentObject currentParent = getGroup(groupPosition);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //the first row is used as header
    if(childPosition ==0)
    {
        view = inflater.inflate(R.layout.child_header, null);
        TextView txtHeader = (TextView)view.findViewById(R.id.txtHeader);
        txtHeader.setText(currentParent.textToHeader);
    }

    //Here is the ListView of the ChildView
    if(childPosition>0 && childPosition<getChildrenCount(groupPosition)-1)
    {
        ChildObject currentChild = getChild(groupPosition,childPosition-1);
        view = inflater.inflate(R.layout.child_row,null);
        TextView txtChildName = (TextView)view.findViewById(R.id.txtChildName);
        TextView txtChildAge = (TextView)view.findViewById(R.id.txtChildAge);
        txtChildName.setText(currentChild.childName);
        txtChildAge.setText("Age: " + String.valueOf(currentChild.age));
    }
    //the last row is used as footer
    if(childPosition == getChildrenCount(groupPosition)-1)
    {
        view = inflater.inflate(R.layout.child_footer,null);
        TextView txtFooter = (TextView)view.findViewById(R.id.txtFooter);
        txtFooter.setText(currentParent.textToFooter);
    }
    return view;
}

@Override
public boolean isChildSelectable(int i, int i2) {
    return false;
}

}

以下是问题http://robusttechhouse.com/how-to-add-header-footer-to-expandablelistview-childview/的解决方案。希望有所帮助!

答案 2 :(得分:-1)

有一种更简单的解决方案:

只需将“OnClickListener”设置为应用的视图:

View view = inflater.inflate(R.layout.xxx, null);
view.setOnClickListener(new OnClickListener(){
 @Override
public void onClick(View v) {
    //do something
 }
});

很容易解决它的事情!