4

我一直在尝试解决如何将孩子添加到 中的View孩子,但无济于事。ListExpandableListActivity

我可以得到ExpandableListView并调用addFooterView()它,但这只是将新添加View到组列表中。(数据来自 a Cursor)。

任何建议都非常感激。

干杯

詹姆士

4

3 回答 3

15

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

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

于 2011-01-04T20:58:22.717 回答
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/。希望有帮助!

于 2015-06-07T03:17:01.013 回答
-1

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

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

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

解决它的非常简单的事情!

于 2013-09-04T22:09:14.650 回答