先说下我这面的需求:
项目中用到了ExpandableListView,原本是两种方案,
一.两级数组同时填充(我开始这样做的,后来因为数据量太大就改了)
二.进入页面返回组数据,然后点击组的条目时候在请求里面的子数据
下面直接就上代码和编写的过程了(按照方案二来做的)。
上效果图
1.首先是获取
组上的时间数据,这里访问一个接口,返回一组数据,然后进行一次赋值,添加到集合里面
private List<String> gruops = new ArrayList<String>();
2.完成后要对组下面的子集和进行初始化(这个必须要有,不然会奔溃额
![惊恐](http://static.blog.csdn.net/xheditor/xheditor_emot/default/panic.gif)
//初始化数据
for (int i = 0; i < gruops.size(); i++) {
childs.add(new LinkedList<MatchEntity>());
}
3.我这里是在点击组条目时候去请求网络查询子条目的数据,并添加到子条目的集合中。
private List<LinkedList<MatchEntity>> childs = new ArrayList<LinkedList<MatchEntity>>();
下面这比较关键了,在添加的时候要在对应的位置上添加
List<MatchEntity> match = result.getData().getMatch();
childs_1 = new LinkedList<MatchEntity>();
childs_1.addAll(match);
childs.set(i, childs_1);
initDateGruop();//这是填充数据
elv_football_single.expandGroup(i, false);//这里是控制展开和收起的作用一会说
4.下面是开始要监控点击事件了,这里我是做了一点其他处理,就是如果集合里有数据的情况下是不去请求网络的。
elv_football_single.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
boolean expanded = parent.isGroupExpanded(groupPosition);
System.out.println("--------"+expanded);
if (!expanded) {
// 在这加载数据,推荐使用AsycnTask之类的在另一个线程里做喽
if (childs.get(groupPosition).size()>0) {
initDateGruop();
return false;
}else{
sendHttpLotteryHistory(
resultDate.getData().get(groupPosition)
.getMatchDate(), wLotteryId, groupPosition);
return true;
}
}
return false;
}
});
这里主要是根据数据时候为空来配合展示开合状态,源码里面内容比较多,主要是一个回调的机制,这里不做解释了(因为有点多,还没弄的很通顺),基本上是可以实现这种效果了。
补充一个更新集合的代码
if (footballAdapter == null) {
footballAdapter = new FootballAdapter(this, gruops, childs);
elv_football_single.setAdapter(footballAdapter);
} else {
footballAdapter.notifyDataSetChanged();
}
补充去除系统自带的上下箭头,在xml中写:
android:groupIndicator="@null"
右面的那个按钮我是直接加在了组条目的布局文件中,做了一个选择器,这里就不多说了。
以上基本就是流程了,过程中还是会有比较多的问题,奔溃的很多次才完善的。如有问题欢迎指教