RecyclerView.Adapter封装,包括点击监听,长按监听,继承时不需要重新设置

自己封装个RecyclerView.Adapter,自己的才是最好的,恩,直接上代码:
1.首先是RecyclerView的ViewHolder:

import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;

/**
 * RecyclerView.ViewHolder万能
 * Created by ruin
 */
public abstract class BaseViewHolder extends ViewHolder {
    public View itemView;

    public BaseViewHolder(View view) {
        super(view);
        itemView = view;
    }
}

大家发现我在封装这个的时候加了个itemView,别困惑~别着急~后面会解释干什么用的~

2.然后我们来封装Adapter

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;
import java.util.List;

/**
 * RecyclerAdapter 封装类
 * Created by ruin
 */
public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter {
    protected Context context;
    protected LayoutInflater inflater;
    protected List mDatas = new LinkedList();
    public OnItemClickListener onItemClickListener;
    public OnItemLongClickListener onItemLongClickListener;

    public BaseRecyclerAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public T getItem(int position) {

        return mDatas.get(position);
    }

    public void addAll(List datas) {
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public void addItemTop(List datas) {
        mDatas = datas;
    }

    public void remove(int position) {
        mDatas.remove(position);
        notifyDataSetChanged();
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    public List getDatas() {
        return mDatas;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return getHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
    //还记得么~上面的itemView~用在这里啦
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position, getItem(position));
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null) {
                    onItemLongClickListener.onItemLongClick(v, position, getItem(position));
                    return true;
                }
                return false;
            }
        });
        setDate(holder, position);
    }

    //继承类的时候只需要重写这个方法,做以前在onBindViewHolder方法里的事情就可以了
    protected abstract void setDate(BaseViewHolder holder, int position);

    //做onCreateViewHolder的事情
    protected abstract BaseViewHolder getHolder(ViewGroup parent, int viewType);

    // 点击事件接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position, T model);
    }

    // 长按事件接口
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position, T model);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
}

你可能感兴趣的:(Android工具类)