How to add footer to child list in ListActivity extension?

I'm trying to figure out how to add a child of a View to a child of a List in ExpandableListActivity , but to no avail.

I can get an ExpandableListView and call addFooterView() on it, but that just adds a new View to the group list. (Data taken from Cursor ).

Any suggestions gratefully received.

Greetings

James

+4
source share
3 answers

Unfortunately, there is no ExpandableListView.addChildFooterView() method or even ExpandableListView.addFooterView() . The method you call is an inherited ListView.addFooterView() , which simply adds the view as the last element to the entire list. This has not been abolished for group / child behavior.

The easiest way to do this is to simply create something in your ExpandableListAdapter implementation so that getChildrenCount() returns the dataset size + 1 and then returns the footer view to getChildView() for the corresponding position. In addition, getChildView() provides a boolean for the flag when it retrieves the last child in a particular group.

+14
source

I made a custom adapter like this and then passed my instance to my 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; } 

}

Here is a solution to the problem http://robusttechhouse.com/how-to-add-header-footer-to-expandablelistview-childview/ . Hope this helps!

+1
source

There is a simpler solution:

Just set "OnClickListener" to the application View:

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

A very simple thing that decided it!

-1
source

Source: https://habr.com/ru/post/1333515/


All Articles