package com.miraclegarden.smsmessage.comm;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * 通用列表适配器
 * @param <T>
 */
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<ViewHolder> {

    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;

    ViewHolder viewHolder;


    public CommonAdapter(Context context, int layoutId, List<T> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        mDatas = datas;
    }
    public void setDates(List<T> dates){
        this.mDatas=dates;
//        notifyItemRangeChanged(0,mDatas.size());
        notifyDataSetChanged();
    }
    public void addDates(List<T> dates){
        this.mDatas.addAll(dates);
        notifyDataSetChanged();
    }

    public void addDates(int localSize){
        int size=mDatas.size();
        notifyItemRangeChanged(size,localSize);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        viewHolder = ViewHolder.get(mContext, parent, mLayoutId);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        convert(holder, mDatas.get(position),position);
    }

    public abstract void convert(ViewHolder holder, T t,int index);


    @Override
    public int getItemCount() {
        return mDatas.size();
    }
}
