最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android ListView中動態(tài)顯示和隱藏Header&Footer的方法

 更新時間:2016年08月26日 16:06:01   作者:生命壹號  
這篇文章主要介紹了Android ListView中動態(tài)顯示和隱藏Header&Footer的方法及footer的兩種正確使用方法,本文介紹的非常詳細,具有參考借鑒價值,對listview header footer相關(guān)知識感興趣的朋友一起學習吧

ListView的模板寫法

ListView模板寫法的完整代碼:

•android代碼優(yōu)化----ListView中自定義adapter的封裝(ListView的模板寫法)

以后每寫一個ListView,就這么做:直接導入ViewHolder.java和ListViewAdapter,然后寫一個自定義adapter繼承自ListViewAdapter就行了。

ListView中動態(tài)顯示和隱藏Header&Footer

如果需要動態(tài)的顯示和隱藏footer的話,按照慣例,誤以為直接通過setVisibility中的View.GONE就可以實現(xiàn)。但是在實際使用中發(fā)現(xiàn)并不是這樣的。

例如,先加載footer布局:

private View mFooter;
mFooter = LayoutInflater.from(this).inflate(R.layout.footer, null); //加載footer的布局
mListView.addFooterView(mFooter);

如果想動態(tài)隱藏這個footer,慣性思維是直接設(shè)置footer為gone:(其實這樣做是不對的)

mFooter.setVisibility(View.GONE); //隱藏footer

實際上,直接設(shè)置GONE后,雖然元素是隱藏了,但是還是占用著那個區(qū)域,此時和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當中
mFooterParent.setOnClickListener(MainActivity.this); //綁定監(jiā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當中

當需要隱藏footer的時候,設(shè)置footer為gone:(不是設(shè)置footerParent為gone)

mFooter.setVisibility(View.GONE);

以上所述是小編給大家介紹的Android ListView中動態(tài)顯示和隱藏Header&Footer的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android通過實現(xiàn)GridView的橫向滾動實現(xiàn)仿京東秒殺效果

    Android通過實現(xiàn)GridView的橫向滾動實現(xiàn)仿京東秒殺效果

    這篇文章主要介紹了Android通過實現(xiàn)GridView的橫向滾動實現(xiàn)仿京東秒殺效果,實現(xiàn)代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • Android實現(xiàn)動態(tài)高斯模糊效果

    Android實現(xiàn)動態(tài)高斯模糊效果

    在Android開發(fā)中常常會用到高斯模糊,但有的時候我們可能會需要一個圖片以不同的模糊程度展現(xiàn)出來,那如何實現(xiàn)呢,一起通過本文來學習學習吧。
    2016-08-08
  • android canvas使用line畫半圓

    android canvas使用line畫半圓

    這篇文章主要為大家詳細介紹了android canvas使用line畫半園,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Android LayerDrawable使用實例

    Android LayerDrawable使用實例

    這篇文章主要介紹了Android LayerDrawable使用實例,本文講解了LayerDrawable的作用、LayerDrawable的原理、LayerDrawableLayerDrawable的使用實例等,需要的朋友可以參考下
    2015-06-06
  • 詳解Android事件的分發(fā)、攔截和執(zhí)行

    詳解Android事件的分發(fā)、攔截和執(zhí)行

    這篇文章主要為大家詳細介紹了詳解Android事件的分發(fā)、攔截和執(zhí)行,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android仿360懸浮小球自定義view實現(xiàn)示例

    Android仿360懸浮小球自定義view實現(xiàn)示例

    本篇文章主要介紹了Android仿360懸浮小球自定義view實現(xiàn)示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Android 實現(xiàn)懸浮窗功能

    Android 實現(xiàn)懸浮窗功能

    這篇文章主要介紹了Android 實現(xiàn)懸浮窗功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Android?Kotlin全面詳細類使用語法學習指南

    Android?Kotlin全面詳細類使用語法學習指南

    這篇文章主要為大家介紹了Android?Kotlin全面詳細類使用語法學習指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Android開發(fā)自學筆記(三):APP布局上

    Android開發(fā)自學筆記(三):APP布局上

    這篇文章主要介紹了Android開發(fā)自學筆記(三):APP布局上,本文講解了添加ViewGroup、添加ViewGroup、定義string內(nèi)容、添加Button、運行程序查看效果等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Android實現(xiàn)繪制LocationMarkerView圖的示例代碼

    Android實現(xiàn)繪制LocationMarkerView圖的示例代碼

    LocationMarker是運動軌跡上Start、End, 以及整公里點上筆者自定義繪制的一個MarkerView。這篇文章主要介紹了Android實現(xiàn)繪制LocationMarkerView圖的示例代碼,希望對大家有所幫助
    2023-02-02

最新評論

鸡泽县| 丽水市| 山阴县| 阳东县| 黄石市| 庐江县| 孟州市| 满城县| 阜康市| 民勤县| 河池市| 于都县| 白水县| 兰溪市| 吴堡县| 望奎县| 自治县| 长沙县| 额敏县| 滨州市| 广安市| 新巴尔虎右旗| 夏津县| 凭祥市| 吴桥县| 富阳市| 南召县| 开平市| 惠州市| 洛阳市| 安福县| 宜章县| 德州市| 伊春市| 三都| 门源| 林周县| 大英县| 明溪县| 景东| 左贡县|