1. 程式人生 > >ListView中動態顯示隱藏HeaderView和FooterView

ListView中動態顯示隱藏HeaderView和FooterView

roi ron mas relative 監聽事件 isp 刪除 listen 具體實現

ListView中動態顯示和隱藏Header&Footer

解決思路: 直接設置HeaderView和FooterView.setVisibility(View.GONE)無效, 布局仍然存在, 需要給布局設置父布局, 然後通過控制子布局來顯示隱藏布局.

  1. 給最外層布局, 嵌套父布局, 通過控制子布局進而控制整個布局;

  2. 給整個布局在代碼中動態添加一個父布局, 然後頭尾部添加父布局,可以直接操控該布局;

具體實現如下

技術分享話外篇:
1.什麽是陰影效果
2.fading:漸變,衰退 fadingEdge:漸變邊緣,衰退邊緣

一、刪除android ScrollView邊界陰影方法方法
1) 在xml中添加:android:fadingEdge=”none” 2) 代碼中添加:ScrollView.setHorizontalFadingEdgeEnabled(false); 二、刪除ScrollView拉到盡頭(頂部、底部),然後繼續拉出現的陰影效果 適用於2.3及以上的 否則不用設置 android:overScrollMode="never"
除去ScrollVIew拉到盡頭時再拉的陰影效果

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

例如,先加載footer布局:

private View mFooter;

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

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

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

實際上,直接設置GONE後,雖然元素是隱藏了,但是還是占用著那個區域,此時和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>
View Code

(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); //綁定監聽事件,點擊查看全部列表

(3)設置footer為gone:(不是設置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的時候,設置footer為gone:(不是設置footerParent為gone)

mFooter.setVisibility(View.GONE);

ListView中動態顯示隱藏HeaderView和FooterView