适配器继承了BaseExpandableListAdapter,ExpandableListView继承了ListView,正常来说,加载出数据,然后通过notifyDataSetChanged()刷新就可以
但BaseExpandableListAdapter更新数据比较恶心
通过handler来刷新数据,而且必须重新伸缩之后才会刷新数据
package com.chunhui.moduleperson.adapter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.chunhui.moduleperson.R;
import com.chunhui.moduleperson.pojo.DateGroup;
import com.chunhui.moduleperson.pojo.SystemMessageInfo;
import com.chunhui.moduleperson.pojo.SystemMsgInfo;
import com.chunhui.moduleperson.widget.PinnedHeaderExpandableListView;
import java.util.ArrayList;
import java.util.List;
/**
* 功能描述:
* Created on 2020/8/11.
*
* @author lyw
*/
public class SystemMessageListAdapter extends BaseExpandableListAdapter {
private List<DateGroup> mDateGroups;
private List<List<SystemMsgInfo>> mSystemMsgInfos;
private Context mContext;
private OnSystemMessageItemClickListener mOnItemClickListener;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
notifyDataSetChanged();//更新数据
super.handleMessage(msg);
}
};
public SystemMessageListAdapter(Context context) {
mContext = context;
mDateGroups = new ArrayList<>();
mSystemMsgInfos = new ArrayList<>();
}
@Override
public int getGroupCount() {
return mDateGroups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mSystemMsgInfos.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return mDateGroups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mSystemMsgInfos.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
...
return convertView;
}
@Override
public View getChildView(final int groupPosition,final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
...
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public void setOnItemClickListener(OnSystemMessageItemClickListener listener){
mOnItemClickListener = listener;
}
public interface OnSystemMessageItemClickListener{
void onItemClick(SystemMsgInfo info, int position);
}
//设置数据
public void setSystemMsgInfos(List<String> timeList,List<List<SystemMsgInfo>> mSystemMsgInfos){
for (int i = 0; i < timeList.size(); i++) {
mDateGroups.add(new DateGroup(timeList.get(i)));
}
this.mSystemMsgInfos.addAll(mSystemMsgInfos);
}
/*供外界更新数据的方法*/
public void refresh(PinnedHeaderExpandableListView mExpandableListView, List<String> timeList){
handler.sendMessage(new Message());
//必须重新伸缩之后才能更新数据
for (int i = 0; i < timeList.size(); i++) {
mExpandableListView.collapseGroup(i);
}
for (int i = 0; i < timeList.size(); i++) {
mExpandableListView.expandGroup(i);
}
}
}
关键代码:
handler 刷新数据
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
notifyDataSetChanged();//更新数据
super.handleMessage(msg);
}
};
/*供外界更新数据的方法*/
public void refresh(PinnedHeaderExpandableListView mExpandableListView, List<String> timeList){
handler.sendMessage(new Message());
//必须重新伸缩之后才能更新数据
for (int i = 0; i < timeList.size(); i++) {
mExpandableListView.collapseGroup(i);
}
for (int i = 0; i < timeList.size(); i++) {
mExpandableListView.expandGroup(i);
}
}
activity调用
获取数据
...
设置数据
mAdapter.setSystemMsgInfos(timeList,mSystemMsgInfos);
刷新数据
mAdapter.refresh(mExpandableListView,timeList);