工作中需要使用ExpandableListView的特性,并且同时又需要支持浮动标题。android系统原生并没有符合要求的控件。通过查询多个第三方开源项目,也没发现特别符合要求的项目。最后绝对参考PinnedHeaderListView项目修改符合自己项目要求的控件,这里要感谢该源代码所有者的分享精神,提供了最基础的思路。
首先将PinnedHeaderListView的父类,直接改为ExpandableListView,非常幸运,并没有任何异常,当然setAdapter等接口就必须遵循ExpandableListView的使用规则,否者会有异常。
PinnedHeaderListView有一个最大的问题,他的浮动标题是不能支持触控事件的,针对这个,首先修改listview页面的布局文件,使用framelayout作为容器,然后将我们作为浮动标题的headerView添加到该容器中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/header_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.widget.PinnedHeaderListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_h