例如,先加載footer佈局:php
private View mFooter; mFooter = LayoutInflater.from(this).inflate(R.layout.footer, null); //加載footer的佈局 mListView.addFooterView(mFooter);
若是想動態隱藏這個footer,慣性思惟是直接設置footer爲gone:(其實這樣作是不對的)java
mFooter.setVisibility(View.GONE); //隱藏footer
實際上,直接設置GONE後,雖然元素是隱藏了,可是仍是佔用着那個區域,此時和View.INVISIBILE效果同樣。android
footer的正確使用方法以下:佈局
一、方法一:this
(1)佈局文件:在footer佈局文件的最外層再套一層LinearLayout/RelativeLayout,咱們稱爲footerParent。spa
layout_footer_listview.xml:(完整版代碼)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="查看更多" android:textColor="#ff0000" android:textSize="20sp"/> </LinearLayout> </LinearLayout>
(2)加載footer和footerParent的佈局:xml
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);
二、方法二:或者直接在代碼中爲footer添加footerParent也能夠,以下:utf-8
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);