com.diegodobelo.expandingview.ExpandingList是一个很好用也很简单的实现带动画的层级列表的第三方UI组件。官方有用法介绍, https://github.com/diegodobelo/AndroidExpandingViewLibrary 。这里用中文再尽可能简洁地讲述一遍用法,并在细节上进行一些补充。
添加依赖什么的配置问题就不说了。
要创建一个ExpandingList,一般只需要五类布局文件。
一类是显示该列表的Activity的布局文件,在需要的地方放上
<com.diegodobelo.expandingview.ExpandingList
android:id="@+id/expanding_list_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
这个顾名思义就是最终加载所有列表项的视图
第二类布局文件是标题项,也就是第一层项的布局文件,根节点用
<com.diegodobelo.expandingview.ExpandingItem
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/actionBarColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:animation_duration="250"
app:item_layout="@layout/mine_expanding_item"
app:separator_layout="@layout/separator_layout"
app:show_animation="true"
app:start_collapsed="true"
app:sub_item_layout="@layout/mine_expanding_sub_item"/>
即可。注意这个节点就是每一个标题项的容器,尤其注意几个属性;
item_layout:具体标题项的布局
separator_layout:每项之间的分割线布局
sub_item_layout:每一个标题项子项的布局
相应的,剩下三类布局文件就是上述三个属性的布局,一一设计创建一下。并回到ExpandingItem这里把这三个属性设置一下(当然后两个也可以缺省).设置好了之后。在ExpandingList所在的java文件中获取到expandingList对象list,后根据需要直接调用下面几个方法
ExpandingItem item = list.createNewItem(R.layout.yourItemLyout)//注意这个布局文件是那个只有一个ExpandingItem根节点的布局文件
// ************************利用item.findViewById(R.id.yourView)************
//对标题项进行一些动态的操作
//****************************************************************************
View subItem = item.createSubItem();//获取子项的根节点
//*****************************再利用subItem.findViewById(R.id.yourSubView'sView);**********
//对每一个标题项的子项也做些动态的操作
这是最简洁的初级用法,还有些高级用法和有用的属性可以去官网看。