Android ListView中動(dòng)態(tài)顯示和隱藏Header&Footer的方法
ListView的模板寫(xiě)法
ListView模板寫(xiě)法的完整代碼:
•android代碼優(yōu)化----ListView中自定義adapter的封裝(ListView的模板寫(xiě)法)
以后每寫(xiě)一個(gè)ListView,就這么做:直接導(dǎo)入ViewHolder.java和ListViewAdapter,然后寫(xiě)一個(gè)自定義adapter繼承自ListViewAdapter就行了。
ListView中動(dòng)態(tài)顯示和隱藏Header&Footer
如果需要?jiǎng)討B(tài)的顯示和隱藏footer的話,按照慣例,誤以為直接通過(guò)setVisibility中的View.GONE就可以實(shí)現(xiàn)。但是在實(shí)際使用中發(fā)現(xiàn)并不是這樣的。
例如,先加載footer布局:
private View mFooter; mFooter = LayoutInflater.from(this).inflate(R.layout.footer, null); //加載footer的布局 mListView.addFooterView(mFooter);
如果想動(dòng)態(tài)隱藏這個(gè)footer,慣性思維是直接設(shè)置footer為gone:(其實(shí)這樣做是不對(duì)的)
mFooter.setVisibility(View.GONE); //隱藏footer
實(shí)際上,直接設(shè)置GONE后,雖然元素是隱藏了,但是還是占用著那個(gè)區(qū)域,此時(shí)和View.INVISIBILE效果一樣。
footer的正確使用方法如下:
1、方法一:
(1)布局文件:在footer布局文件的最外層再套一層LinearLayout/RelativeLayout,我們稱為footerParent。
layout_footer_listview.xml:(完整版代碼) <?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="查看更多" android:textColor="#ff0000" android:textSize="20sp"/> </LinearLayout> </LinearLayout>
(2)加載footer和footerParent的布局:
private View mFooter; //footer private View mFooterParent; //footer的最外面再套一層LinearLayout mFooterParent = LayoutInflater.from(getActivity()).inflate(R.layout.footerparent_listview, null);//加載footerParent布局 mFooter = mFooterParent.findViewById(R.id.footer); listView.addFooterView(mFooterParent); //把footerParent放到ListView當(dāng)中 mFooterParent.setOnClickListener(MainActivity.this); //綁定監(jiān)聽(tīng)事件,點(diǎn)擊查看全部列表
(3)設(shè)置footer為gone:(不是設(shè)置footerParent為gone)
mFooter.setVisibility(View.GONE);
2、方法二:
或者直接在代碼中為footer添加footerParent也可以,如下:
private View mFooter; //footer mFooter = LayoutInflater.from(getActivity()).inflate(R.layout.footer_listview, null);//加載footer布局 LinearLayout mFooterParent = new LinearLayout(context); mFooterParent.addView(mFooter);//在footer的最外面再套一層LinearLayout(即footerParent) listView.addFooterView(mFooterParent);//把footerParent放到ListView當(dāng)中
當(dāng)需要隱藏footer的時(shí)候,設(shè)置footer為gone:(不是設(shè)置footerParent為gone)
mFooter.setVisibility(View.GONE);
以上所述是小編給大家介紹的Android ListView中動(dòng)態(tài)顯示和隱藏Header&Footer的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android通過(guò)json向MySQL中讀寫(xiě)數(shù)據(jù)的方法詳解【寫(xiě)入篇】
這篇文章主要介紹了Android通過(guò)json向MySQL中讀寫(xiě)數(shù)據(jù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android json類的定義、調(diào)用及php接收json數(shù)據(jù)并寫(xiě)入mysql的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法,涉及Android日期時(shí)間相關(guān)轉(zhuǎn)換與運(yùn)算操作技巧,需要的朋友可以參考下2018-01-01解決Eclipse創(chuàng)建android項(xiàng)目無(wú)法正常預(yù)覽布局文件問(wèn)題的方法
這篇文章主要介紹了解決Eclipse創(chuàng)建android項(xiàng)目無(wú)法正常預(yù)覽布局文件問(wèn)題的方法,需要的朋友可以參考下2015-12-12Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11android 手機(jī)SD卡讀寫(xiě)操作(以txt文本為例)實(shí)現(xiàn)步驟
要完成SD卡讀寫(xiě)操作首先對(duì)manifest注冊(cè)SD卡讀寫(xiě)權(quán)限其次是創(chuàng)建一個(gè)對(duì)SD卡中文件讀寫(xiě)的類寫(xiě)一個(gè)用于檢測(cè)讀寫(xiě)功能的的布局然后就是UI的類了,感興趣的朋友可以參考下,希望可以幫助到你2013-02-02跨平臺(tái)移動(dòng)WEB應(yīng)用開(kāi)發(fā)框架iMAG入門教程
這篇文章主要介紹了跨平臺(tái)移動(dòng)WEB應(yīng)用開(kāi)發(fā)框架iMAG入門教程,iMAG最大的特點(diǎn)是生成各移動(dòng)平臺(tái)的原生代碼,需要的朋友可以參考下2014-07-07