ExpandableListView添加頭佈局和尾部局

ExpandableListView是什麼?

ExpandableListView時一種可摺疊的下拉列表,就像之前QQ好友分組的控件。

ExpandableListView怎麼用?

它和ListView的用法差不多,寫Adapter時繼承的是BaseExpandableListAdapter,重寫裏邊的方法。

ExpandableListView添加頭佈局和尾部局?

不知道大家看過ListView添加頭佈局和尾部局的方法沒,其實ExpandableListView的添加方法和ListView的添加方法是一樣的,下面詳細說明:

1.ExpandableListView可以展示後,新建一個shopping_footer_layout.xml佈局,講佈局設置好,如果只有一個控件,它可以直接設置爲根部局。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_footer"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/aliceblue"
    android:text="---隨便逛逛---"
    android:gravity="center"
    android:clickable="true"
    >
</TextView>

2.在Activity中實例化。

//exListView的尾部局
    private TextView tv_footer;
    tv_footer = (TextView) LayoutInflater.from(this).inflate(R.layout.shopping_footer_layout, null);
3.添加頭尾佈局,ListView是在Adapter設置之前添加,ExpandableListView我試驗了,在Adapter之前或之後添加都可以。

//添加尾部局
        exListView.addFooterView(tv_footer);
//或者添加頭佈局
        exListView.addHeaderView(tv_footer);
4.給頭佈局/尾部局設置點擊事件,跟正常的一樣。
 tv_footer.setOnClickListener(this);

出現的問題

無論你怎樣改shopping_footer_layout.xml中的高度,運行出來隨便逛逛那部分的高度不變,但是這部分又要寬一點,我想到了另一種方法,在TextView中加一個padding屬性

多高自己隨便改。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_footer"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/aliceblue"
    android:text="---隨便逛逛---"
    android:gravity="center"
    android:clickable="true"
    android:padding="30dp"
    >



發佈了45 篇原創文章 · 獲贊 49 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章