Fixed and always visible footer below ListFragment
Asked Answered
M

1

16

I'm trying attach a footer, that is fixed and always visible, to the bottom of a ListFragment.

I'm currently doing it like this:

@Override public void onActivityCreated(Bundle savedInstanceState) {

    // ...

    adapter = new MyAdapter(getActivity(), R.layout.list, dataList);

    ListView list = getListView();
    View footer = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_add, null, false);

    list.addFooterView(footer);
    setListAdapter(adapter);
}

While this code does produce a view at the bottom of the list, it doesn't really do what I want:

First, I need the footer to be FIXED, i.e., visible on the screen regardless of where the list is scrolled. With this solution, the footer is only visible when the screen is scrolled to the bottom of the list.

Second, I need the footer to appear even when the list is EMPTY. In this solution, the footer is not visible when the list is empty.

What is the best way to get a fixed footer (in my case, a button) to always appear below a ListFragment or ListActivity?

Thanks!

Mercier answered 10/9, 2012 at 14:24 Comment(0)
S
42

You can do that in the xml layout:

<RelativeLayout>

    <Button android:id="@+id/footer" android:layout_alignParentBottom="true"/> 
    <ListView android:id="@android:id/list" android:layout_above="@id/footer"> <!-- the list -->

</RelativeLayout>

This layout will be used in the onCreateView method of the fragment.

Strade answered 10/9, 2012 at 14:40 Comment(9)
Would this work even if I'm using a ListFragment, rather than a plain old Fragment? Or would I have to convert it to a Fragment?Mercier
@Mercier A ListFragment is a fragment for which the View is a simple ListView with the id android.R.id.list. You can still use the ListFragment, you just need to override the onCreateView method and return the layout from my answer. Th rest of your code from the current ListFragment will remain the same.Strade
I modified R.layout.footer_add to have a RelativeLayout with the ListView and the footer Button. Does the overridden onCreateView method get the layout using findViewById on R.layout.footer_add, or by inflating it? I'm getting an error either way.Mercier
@Mercier The onCreateView should be like this: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.the_layout, container, false); } .Strade
WORKS, awesome! My mistake was leaving in the dynamic call to addFooterView(). Thanks a million!Mercier
Quick follow up question. Your solution works great, but the rows of the list are added starting at the bottom of the page, just above the footer button. Is there a way to keep the button on the bottom, but have the list rows attach starting at the top?Mercier
@Mercier I'm not sure I understand. See if you didn't set the transcript mode(the method setTranscriptMode from the ListView widget). Or add the attribute android:layout_alignParentTop="true" for the ListView in the layout file.Strade
Looks like I forgot the android:layout_alignParentTop="true" on the ListView. Works now, thanks again!!Mercier
listview width and height should be match_parent...I was facing issue here..thanksMcclish

© 2022 - 2024 — McMap. All rights reserved.