ExpandableListView使用,点击条目请求网络数据,箭头在右边
2016-05-19 16:02
671 查看
先说下我这面的需求:
项目中用到了ExpandableListView,原本是两种方案,
一.两级数组同时填充(我开始这样做的,后来因为数据量太大就改了)
二.进入页面返回组数据,然后点击组的条目时候在请求里面的子数据
下面直接就上代码和编写的过程了(按照方案二来做的)。
上效果图
![](https://img-blog.csdn.net/20160519160647643?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160519160737497?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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"右面的那个按钮我是直接加在了组条目的布局文件中,做了一个选择器,这里就不多说了。
以上基本就是流程了,过程中还是会有比较多的问题,奔溃的很多次才完善的。如有问题欢迎指教
项目中用到了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"右面的那个按钮我是直接加在了组条目的布局文件中,做了一个选择器,这里就不多说了。
以上基本就是流程了,过程中还是会有比较多的问题,奔溃的很多次才完善的。如有问题欢迎指教
相关文章推荐
- Android ExpandableListView长按事件的完美解决办法
- Android中ExpandableListView的用法实例
- Android ExpandableListView展开列表控件使用实例
- 分享Android中ExpandableListView控件使用教程
- Android改变ExpandableListView的indicator图标实现方法
- android xml注意事项
- ExpandableListView的首次加载全部展开,并且点击Group不收缩、
- 用迭代法 解析 json 格式 数据
- ExpandableListView及其子项显示上下文菜单
- Android,三级列表实现
- xml文件的注意事项
- android聊天器(仿QQ)编写第一天__界面的搭建和主要的布局
- ExpandableListView 嵌套gridView使用
- ExpandableListView的用法
- ExpandableListView实现多级菜单
- ExpandableListView 的几个有用点的属性
- ScrollView中嵌套ListView/ExpandableListView显示问题
- ExpandableListView 默认打开 点击不可收回
- ExpandableListView组件中CheckBox全选的使用(使用观察者模式)
- ExpandableListView