SoFunction
Updated on 2025-03-11

How to dynamically display and hide Header&Footer in Android ListView

ListView template writing

The complete code of the ListView template writing method:

•Android code optimization-----Encapsulation of custom adapters in ListView (Template writing of ListView)

In the future, every time you write a ListView, just import and ListViewAdapter directly, and then write a custom adapter to inherit from ListViewAdapter.

Dynamic display and hide Header&Footer in ListView

If you need to display and hide footers dynamically, according to convention, you mistakenly think that it can be achieved directly through the setVisibility. However, it is not found in actual use.

For example, first load the footer layout:

private View mFooter;
mFooter = (this).inflate(, null); //Load footer layout(mFooter);

If you want to hide this footer dynamically, inertial thinking is to directly set footer to gone: (In fact, it is wrong to do this)

(); //hidefooter

In fact, after setting GONE directly, although the element is hidden, it still occupies that area, and the effect is the same as the one at this time.

The correct way to use footer is as follows:

1. Method 1:

(1) Layout file: Put another layer of LinearLayout/RelativeLayout on the outermost layer of the footer layout file, which we call footerParent.

layout_footer_listview.xml:(Full version code)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:andro
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
>
<LinearLayout
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="See More"
android:textColor="#ff0000"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>

(2) Load the layout of footer and footerParent:

private View mFooter; //footer
private View mFooterParent; //The outermost part of the footer is equipped with another layer of LinearLayoutmFooterParent = (getActivity()).inflate(.footerparent_listview, null);//Load footerParent layoutmFooter = ();
(mFooterParent); //Put footerParent into ListView(); //Binding listening events,Click to view all lists

(3) Set footer to gone: (not setting footerParent to gone)

();

2. Method 2:

Or you can add footerParent to footer directly in the code, as follows:

private View mFooter; //footer
mFooter = (getActivity()).inflate(.footer_listview, null);//Load footer layoutLinearLayout mFooterParent = new LinearLayout(context); 
(mFooter);//Put another layer of LinearLayout (i.e. footerParent)(mFooterParent);//BundlefooterParentPut it inListViewamong

When you need to hide footer, set footer to gone: (not setting footerParent to gone)

();

The above is the method of dynamic display and hiding Header&Footer in Android ListView introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!