Fixed and always visible footer below ListFragment

痞子三分冷 提交于 2019-12-18 14:38: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!


回答1:


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.



来源:https://stackoverflow.com/questions/12353701/fixed-and-always-visible-footer-below-listfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!