关于ExpandableListView刷新的解决办法

前言

最近在做项目, 用到ExpandableListView 是很正常的是吧? 因为ExpandableListView 真的是一个不错的Widget! 但它的刷新问题真系很恶心! 在网上找了很多资料, 但都是很多的问题! 现在我弄出了一种方法, 估计是没问题的!

出处

http://blog.csdn.net/arylo/article/details/8711769

正文

首先是最基础的

 

ExpandableListView vList = (ExpandableListView) this.findViewById(R.id.list);
EListAdapter adapter = new EListAdapter(getApplicationContext(), list); //list 是数据源
vList.setAdapter(adapter);

//适配器就不写了, 都懂的
class EListAdapter extends BaseExpandableListAdapter {}

 

 

 

一般情况下, ListView是使用notifyDataSetChanged() 更新的

    adapter.notifyDataSetChanged(); 

 
ExpandableListView 也是ListView, 估计这样是可以的.

 

可惜现在用的不是ListView, 而是ExpandableListView! 所以报错了0. 0

java.lang.ClassCastException: android.widget.ExpandableListConnector

 

Google 找了资料, 发现是很多都犯这个错误. 解决方法也蛮简单的!

 

 

 1 class EListAdapter extends BaseExpandableListAdapter {
 2     public EListAdapter(Context context, List<T> list) {
 3         this.list = list;
 4         this.context = context;
 5         handler = new Handler(){
 6 
 7             @Override
 8             public void handleMessage(Message msg) {
 9               notifyDataSetChanged();
10               super.handleMessage(msg);
11             }
12         };
13     }
14     
15     public void refresh() {
16         handler.sendMessage(new Message());
17     }
18 }

 

 

 

只要我们调用refresh() 方法就可以使用notifyDataSetChanged() 了.

 

 

可是!! 只有GroupView 更新!!

ChildView 没有更新! 惨了....要更新的在ChildView!!

继续靠Google 娘! 大家都提供很多方法, 有一个人说, 直接在list 添加item 再更新就可以了! 

我试了一下,没有任何效果.......

查了一查SDK 文档, 发现Group 的伸缩会引起getChildView(int, int, boolean, View, ViewGroup)  的运行!

所以刷新的ChildView 的方法很简单. 

只有伸缩一次就可以了! 连Adapter 都不用重写! 简单吧?

 

  

1 vList.collapseGroup(groupPosition);
2 vList.expandGroup(groupPosition);

转:http://blog.csdn.net/arylo/article/details/8711769

posted @ 2014-09-28 17:10  goee  阅读(2744)  评论(0编辑  收藏  举报