I know how to add a header or a footer in JAVA, but I was wondering if I could add it straight in the XML. I wouldn't want to simulate this, but really add it as footer or header!
-
2Of course you can. Just add them before and after your ListView. Two TextViews could be the simplest headers, but you could add some more complex layouts too. I usually do it so (I put the whole thing in a RelativeLayout). They will be fixed in place and won't scroll withe listView's elements.– PhantômaxxApr 15, 2014 at 12:28
-
Soooo...simply adding them before and after the list is the right way to do it? I thought it was a better way than that...– ScorpioApr 15, 2014 at 12:35
-
2No, it doesn't do EXACTLY what I asked, since I actually want to add another view that will have the attribute android:layout_below="@id/list_id" and some of the lists may or may not have a footer. your solution would not work there. I know how to get past this, by adding the list and footer in another layout, but I was asking for a straightforward solution, not some 'tricks'.– ScorpioApr 16, 2014 at 8:52
-
1Maybe I didn't explain me well. This is no "trick" and it is very straightforward... Jus add in a RelativeLayout a view (header) aligned to parent TOP. Another one (footer) aligned to the parent BOTTOM. And then your ListView both BELOW the header and ABOVE the footer. Get it now? Isn't it straight enough? It really does EXACTLY what you asked.– PhantômaxxApr 16, 2014 at 8:55
-
4What I'm looking for is how to add actual header/footer, which would scroll with other list items, not just a view above/below the list view.– ViliusKJul 11, 2014 at 12:51
4 Answers
No, I don't think that it is possible. Based on ListView source code there are only overScrollHeader/overScrollFooter
are available from XML attributes. But these attributes accept only drawables.
If you don't want to use tricks with layouts above/below ListView
. You can extend ListView
and implement your own footer and header support in customized View
. It is not so hard because of footer and header are already implemented. You only have to add XML attributes parsing in your customized View's constructor.
-
2
I was just trying to achieve the same thing (to keep my code cleaner and use XML for markup & source code for logic), but the only solution I found is to define the header view with XML somewhere in your layout and then detach it and put into ListView as header.
For example, having this XML:
<ListView android:id="@+id/myListView">
</ListView>
<LinearLayout android:id="@+id/myHeader">
....
</LinearLayout>
You can do this in your code:
ListView myListView = (ListView) findViewById(R.id.myListView);
LinearLayout myHeader = (LinearLayout) findViewById(R.id.myHeader);
// Let's remove the myHeader view from it's current child...
((ViewGroup) myHeader.getParent()).removeView(myHeader);
// ... and put it inside ListView.
myListView.addFooterView(myHeader);
Basically, what we do here is just detach the inflated LinearLayout from its parent and set it as ListView header child.
This is not an ideal solution, but it is still easier than creating/inflating header manually. Also this utilizes the power of XML inflation & view reusing if you're using this inside some "holder" pattern.
Hope this helps somebody.
-
3I get a crash when I attempt to use this technique. I had to put the header layout into a separate layout xml.– ThomasWMay 31, 2016 at 7:32
This is how it worked for me, in my Adapter class which extends the BaseAdapter. I am targeting API 23:
@Override
public View getView(int position, View view, ViewGroup parent) {
if (position == 0) {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
view = layoutInflater.inflate(R.layout.test_results_header, parent, false);
}
} else {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
view = layoutInflater.inflate(R.layout.test_result_item, parent, false);
}
}
Pretty simple, I inflate a header XML for position 0 and the content XML for the rest. If you know the position where you want a header or any other XML, in your logic you would need to check the position, and inflate the respective XML for that position.
I created an xml
resource same as my adapter rows xml (so the title is fit) and added it to the listview
after addind the adapter:
listView.setAdapter(myRowsAdapter);
listView.addHeaderView(View.inflate(getContext(), R.layout.title_row, null));