ExpandableListView的刷新问题

适配器继承了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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值