Add a header/footer in a listview in xml
Asked Answered
J

4

21

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!

Jitters answered 15/4, 2014 at 12:25 Comment(6)
Of 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.Anisomerous
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...Jitters
No, 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'.Jitters
Maybe 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.Anisomerous
I admit that I wasn't explicit enough. I may want to add another footer to the list. I would not be able to do that, because I would not know to which element to link it to: the list or the other footer (some lists have footers, some don't). A correct full footer behavior allows for multiple footers to be added or removed programatically. The thing I wanted to do is be able to add footers to a list no matter if it has a footer in the XML or not. Writing list.addFooter(footerView) in JAVA will set the JAVA added footer and the XML added footer in the same space on the screen.Jitters
What 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.Unofficial
K
14

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.

Kamakura answered 15/4, 2014 at 13:48 Comment(1)
Thanks for the answer, I just wanted a second opinion on this!Jitters
L
5

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.

Ligni answered 16/8, 2015 at 18:35 Comment(1)
I get a crash when I attempt to use this technique. I had to put the header layout into a separate layout xml.Rouble
A
0

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.

Areca answered 4/10, 2017 at 14:2 Comment(0)
S
0

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));
Statistician answered 3/1, 2021 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.