Android ListView中動態顯示和隱藏Header&Footer的方法_Android

來源:互聯網
上載者:User

ListView的模板寫法

ListView模板寫法的完整代碼:

•android代碼最佳化----ListView中自訂adapter的封裝(ListView的模板寫法)

以後每寫一個ListView,就這麼做:直接匯入ViewHolder.java和ListViewAdapter,然後寫一個自訂adapter繼承自ListViewAdapter就行了。

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

如果需要動態顯示和隱藏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"?><LinearLayoutxmlns: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"><LinearLayoutandroid:id="@+id/mFooter"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"><TextViewandroid: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; //footerprivate View mFooterParent; //footer的最外面再套一層LinearLayoutmFooterParent = 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; //footermFooter = 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);

以上所述是小編給大家介紹的Android ListView中動態顯示和隱藏Header&Footer的方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.