Skip to content

Instantly share code, notes, and snippets.

@pablisco
Created June 17, 2014 09:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablisco/55e8f6d755e517502161 to your computer and use it in GitHub Desktop.
Save pablisco/55e8f6d755e517502161 to your computer and use it in GitHub Desktop.
How to ensure that we can add a footer to the list in a list fragment

Sometimes we want to add or remove a fragment after the adapter has been set. This is ok on Kit-Kat but if we are working with compatibility libraries to support earlier versions of Android we can use this to guaranty that the internal ListView wraps the adapter in a HeaderViewListAdapter.

This example show how this can easily handled. The same logic can be used with a stand alone ListView in a custom layout.

import android.os.Build;
import android.support.v4.app.ListFragment;
public class HeadListFragment extends ListFragment {
@Override
public void setListAdapter(ListAdapter adapter) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
View fixFooter = new View(getActivity());
getListView().addFooterView(fixFooter);
super.setListAdapter(adapter);
getListView().removeFooterView(fixFooter);
} else {
super.setListAdapter(adapter);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment