正文
首先是最基礎的
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 找了資料, 發現是很多都犯這個錯誤. 解決方法也蠻簡單的!
class EListAdapter extends BaseExpandableListAdapter { public EListAdapter(Context context, List<T> list) { this.list = list; this.context = context; handler = new Handler(){ @Override public void handleMessage(Message msg) { notifyDataSetChanged(); super.handleMessage(msg); } }; } public void refresh() { handler.sendMessage(new Message()); }}只要我們調用refresh() 方法就可以使用notifyDataSetChanged() 了.
可是!! 只有GroupView 更新!!
ChildView 沒有更新! 慘了....要更新的在ChildView!!
繼續靠Google 娘! 大家都提供很多方法, 有一個人說, 直接在list 添加item 再更新就可以了!
我試了一下,沒有任何效果.......
查了一查SDK 文檔, 發現Group 的伸縮會引起getChildView(int, int, boolean, View, ViewGroup) 的運行!
所以刷新的ChildView 的方法很簡單.
只有伸縮一次就可以了! 連Adapter 都不用重寫! 簡單吧?
vList.collapseGroup(groupPosition);vList.expandGroup(groupPosition);
以上就是小編為大家帶來的Android 關于ExpandableListView刷新問題的解決方法全部內容了,希望大家多多支持武林網~
新聞熱點
疑難解答