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中实例化。
3.添加头尾布局,ListView是在Adapter设置之前添加,ExpandableListView我试验了,在Adapter之前或之后添加都可以。//exListView的尾部局 private TextView tv_footer; tv_footer = (TextView) LayoutInflater.from(this).inflate(R.layout.shopping_footer_layout, null);
//添加尾部局
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"
>