BaseRecyclerAdapter.java 2.17 KB
package com.cnlive.im.adapter.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zuoxian on 15/9/7.
 */
public abstract class BaseRecyclerAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    protected List<Object> mItems ;
    protected Context mContext;
    public LayoutInflater inflater;
    public BaseRecyclerAdapter(Context context){
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mItems = new ArrayList<>();
    }

    public List<Object> getList() {
        return mItems;
    }

    public void addItem(@Nullable Object item) {
        if(item == null) return ;
        mItems.add(mItems.size(), item);
        notifyItemInserted(mItems.size());
    }

    public void addItems(@Nullable List<Object> items){
        if(items == null) return ;
        this.mItems.addAll(items);
        notifyDataSetChanged();
    }

    public boolean containsAll(@NonNull List<Object> items){
        return mItems.containsAll(items);
    }

    public void updateItem(@Nullable Object tasks, int position) {
        if(tasks == null) return ;
        mItems.set(position, tasks);
        notifyItemChanged(position);
    }

    public void updateItems(@Nullable List<Object> items){
        if(items == null) return;
        this.mItems.clear();
        this.mItems.addAll(items);
        Log.v("getItemCount",mItems.size()+"");

        notifyDataSetChanged();
    }

    public void removeItem(int index) {
        mItems.remove(index);
        notifyItemRemoved(index);
    }

    public void getView(int position , RecyclerView.ViewHolder viewHolder, int type, Object item){}

    public Object getItem(int location) {
        return mItems.get(location);
    }

    @Override
    public int getItemCount() {
        Log.v("getItemCount",mItems.size()+"");
        return mItems == null ? 0 : mItems.size();
    }

    public void clear(){
        mItems.clear();
        notifyDataSetChanged();
    }

}