The method of dynamically displaying and hiding Header&footer in Android ListView _android

Source: Internet
Author: User

ListView's Template Writing

Complete code for ListView template writing:

Android Code optimization----ListView a Custom Adapter encapsulation (ListView template notation)

After each write a ListView, do this: directly import Viewholder.java and Listviewadapter, and then write a custom adapter inherited from Listviewadapter on the line.

Dynamic display and hiding Header&footer in listview

If you need to dynamically display and hide footer words, according to the Convention, mistakenly think that directly through the setvisibility in the View.gone can be achieved. But this is not the case in practical use.

For example, load the footer layout first:

Private View Mfooter;
Mfooter = Layoutinflater.from (this). Inflate (r.layout.footer, NULL); Load the footer layout
mlistview.addfooterview (mfooter);

If you want to hide this footer dynamically, inertia thinking is directly set footer as gone: (in fact, this is wrong)

Mfooter.setvisibility (View.gone); Hide Footer

In fact, directly after setting gone, although the element is hidden, but still occupy the area, this time and view.invisibile effect.

The correct use of footer is as follows:

1. Method One:

(1) Layout file: The outermost layer of the footer layout file is linearlayout/relativelayout, which we call footerparent.

Layout_footer_listview.xml: (full version code)
<?xml version= "1.0" encoding= "Utf-8"
?> <linearlayout Xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+id/mfooterparent"
android: Layout_width= "Match_parent"
android:layout_height= "wrap_content"
android:background= "#FFFFFF"
android:gravity= "center"
android:orientation= "vertical"
>
<linearlayout
android:id= "@ +id/mfooter "
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=" View More "
android:textcolor=" #ff0000 "
android:textsize=" 20sp "/>
</ Linearlayout>
</LinearLayout>

(2) Load the layout of footer and footerparent:

Private View Mfooter; Footer
Private View mfooterparent//footer the outermost layer of linearlayout
mfooterparent = Layoutinflater.from ( Getactivity ()). Inflate (R.layout.footerparent_listview, NULL);//load footerparent layout
Mfooter = Mfooterparent.findviewbyid (r.id.footer);
Listview.addfooterview (mfooterparent); Put Footerparent into ListView
Mfooterparent.setonclicklistener (Mainactivity.this);//Bind listener events, click to view all lists

(3) Set footer to gone: (not set footerparent to gone)

Mfooter.setvisibility (View.gone);

2. Method Two:

Or you can add footerparent to footer directly in your code, as follows:

Private View Mfooter; Footer
Mfooter = Layoutinflater.from (Getactivity ()). Inflate (R.layout.footer_listview, NULL);//Load Footer layout
linearlayout mfooterparent = new LinearLayout (context); 
Mfooterparent.addview (Mfooter)//The outermost layer of the footer LinearLayout (i.e. footerparent)
Listview.addfooterview ( mfooterparent)/Put Footerparent in ListView

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

Mfooter.setvisibility (View.gone);

The above is a small series to introduce the Android ListView dynamic display and hidden Header&footer method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.