【问题标题】:How do I add a footer View to a child List in a ExpandableListActivity?如何将页脚视图添加到 ExpandableListActivity 中的子列表?
【发布时间】:2010-12-27 22:57:08
【问题描述】:

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

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

任何建议都非常感谢。

干杯

詹姆斯

【问题讨论】:

  • 如果没有您的代码,很难真正判断发生了什么,但是 addFooterView() 的一个警告是您必须在 setAdapter() 之前调用它
  • 谢谢。我已经成功调用了 addFooterView() (正如你所指出的,在设置适配器之后)它只是做了我上面所说的(将它添加到组条目中)。我不确定这里的代码是否真的有帮助。这个问题很简单——我该怎么做?
  • 你需要设置它之前而不是之后。
  • 抱歉,来晚了。我之前确实是这个意思。但问题仍然存在。

标签: android listview expandablelistview


【解决方案1】:

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

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

【讨论】:

  • 谢谢。我在通过源头旅行时发现了这一点。我相信另一种选择是实现某种包装适配器。
  • 我尝试了这种方法,当折叠第一组 childViews 时,它可以工作,但是当折叠下一组 childViews 时,我得到了意外的崩溃行为
【解决方案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/ 问题的解决方案。希望对您有所帮助!

【讨论】:

    【解决方案3】:

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

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

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

    很简单的事情就解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      相关资源
      最近更新 更多