RecyclerView Adapter 封装

用了近一年的recyclerview 这里给出RecyclerView Adapter 封装简化模式

  包括item点击事件 以及局部点击事件,这将会为您的代码提供极高的简便  包括局部添加 绑定 插入 更新等等 


import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * ClassName BaseRecyclerAdapter
 * Description
 * Company 
 * author  youxuan  E-mail:[email protected]
 * date createTime:2015/9/10 10:05
 * version
 */
public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter<BaseRecyclerAdapter.ViewHolder> {


    public BaseRecyclerAdapter() {
    }

    public abstract int bindView(int viewtype);

    public Object getItem(int position) {
        return null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewtype) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(bindView(viewtype), viewGroup, false));
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        private SparseArray<View> holder = null;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        /**
         * 获取子控件
         *
         * @param id
         * @param <T>
         * @return
         */
        public <T extends View> T obtainView(int id) {
            if (null == holder) holder = new SparseArray<>();
            View view = holder.get(id);
            if (null != view) return (T) view;
            view = itemView.findViewById(id);
            if (null == view) return null;
            holder.put(id, view);
            return (T) view;
        }


        public <T> T obtainView(int id, Class<T> viewClazz) {
            View view = obtainView(id);
            if (null == view) return null;
            return (T) view;
        }


        public boolean bindChildClick(int id) {
            View view = obtainView(id);
            if (view == null) return false;
            view.setOnClickListener(this);
            return true;
        }

        /**
         * 子控件绑定局部点击事件
         *
         * @param v
         * @return
         */
        public boolean bindChildClick(View v) {
            if (v == null) return false;
            if (obtainView(v.getId()) == null)
                return false;
            v.setOnClickListener(this);
            return true;
        }


        public boolean bindChildLongClick(int id) {
            View view = obtainView(id);
            if (view == null) return false;
            view.setOnLongClickListener(this);
            return true;
        }

        public boolean bindChildLongClick(View v) {
            if (v == null) return false;
            if (obtainView(v.getId()) == null)
                return false;
            v.setOnLongClickListener(this);
            return true;
        }

        /**
         * 文本控件赋值
         *
         * @param id
         * @param text
         */
        public void setText(int id, CharSequence text) {
            View view = obtainView(id);
            if (view instanceof TextView) {
                ((TextView) view).setText(text);
            }
        }


        @Override
        public boolean onLongClick(View v) {
            if (onItemLongClickListener != null && v.getId() == this.itemView.getId()) {
                onItemLongClickListener.onItemLongClick(BaseRecyclerAdapter.this, this, v, getAdapterPosition());
                return true;
            } else if (onItemChildLongClickListener != null && v.getId() != this.itemView.getId()) {
                onItemChildLongClickListener.onItemChildLongClick(BaseRecyclerAdapter.this, this, v, getAdapterPosition());
                return true;
            }
            return false;
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null && v.getId() == this.itemView.getId()) {
                onItemClickListener.onItemClick(BaseRecyclerAdapter.this, this, v, getAdapterPosition());
            } else if (onItemChildClickListener != null && v.getId() != this.itemView.getId()) {
                onItemChildClickListener.onItemChildClick(BaseRecyclerAdapter.this, this, v, getAdapterPosition());
            }
        }


    }

    protected OnItemClickListener onItemClickListener;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

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


    protected OnItemLongClickListener onItemLongClickListener;

    public OnItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }

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


    public interface OnItemClickListener {
        void onItemClick(BaseRecyclerAdapter adapter, ViewHolder holder, View view, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(BaseRecyclerAdapter adapter, ViewHolder holder, View view, int position);
    }


    public interface OnItemChildClickListener {
        void onItemChildClick(BaseRecyclerAdapter adapter, ViewHolder holder, View view, int position);
    }

    public interface OnItemChildLongClickListener {
        void onItemChildLongClick(BaseRecyclerAdapter adapter, ViewHolder holder, View view, int position);
    }


    protected OnItemChildClickListener onItemChildClickListener;
    protected OnItemChildLongClickListener onItemChildLongClickListener;

    public OnItemChildLongClickListener getOnItemChildLongClickListener() {
        return onItemChildLongClickListener;
    }

    public void setOnItemChildLongClickListener(OnItemChildLongClickListener onItemChildLongClickListener) {
        this.onItemChildLongClickListener = onItemChildLongClickListener;
    }

    public OnItemChildClickListener getOnItemChildClickListener() {
        return onItemChildClickListener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        this.onItemChildClickListener = onItemChildClickListener;
    }


    /**
     * 蓝底 白字
     *
     * @param view
     * @param snack
     */
    public void showSnack(View view, CharSequence snack) {
        if (view == null) return;
        if (TextUtils.isEmpty(snack)) return;
        Snackbar snackbar = Snackbar.make(view, snack, Snackbar.LENGTH_SHORT);
        snackbar.getView().setBackgroundColor(view.getContext().getResources().getColor(R.color.title_background));
        ((TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text)).setTextColor(Color.WHITE);
        snackbar.show();
    }

    /**
     * 蓝底 白字
     *
     * @param view
     * @param resId
     */
    public void showSnack(View view, int resId) {
        if (view == null) return;
        String resStr = view.getContext().getResources().getString(resId);
        if (TextUtils.isEmpty(resStr)) return;
        Snackbar snackbar = Snackbar.make(view, resId, Snackbar.LENGTH_SHORT);
        snackbar.getView().setBackgroundColor(view.getContext().getResources().getColor(R.color.title_background));
        ((TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text)).setTextColor(Color.WHITE);
        snackbar.show();
    }

}


数组的recyclerview adapter:

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

/**
 * ClassName ArrayRecyclerAdapter
 * Description
 * Company 
 * author  youxuan  E-mail:[email protected]
 * date createTime:2015/9/16 9:22
 * version
 */
public abstract class BaseArrayRecyclerAdapter<T> extends BaseRecyclerAdapter {
    public final List<T> dataList = new ArrayList<T>();

    public List<T> getData() {
        return dataList;
    }

    public T getData(int position) {
        if (position < 0 || position >= dataList.size()) return null;
        return dataList.get(position);
    }


    public boolean bindData(boolean isRefresh, List<T> datas) {
        if (datas == null) return false;
        if (isRefresh) dataList.clear();
        boolean b = dataList.addAll(datas);
        notifyDataSetChanged();
        return b;
    }

    public void clearData() {
        dataList.clear();
        notifyDataSetChanged();
    }

    @Override
    public T getItem(int position) {
        if (position < 0 || position >= dataList.size()) return null;
        return dataList.get(position);
    }

    public boolean addItem(int position, T t) {
        if (t == null) return false;
        if (position < 0 || position > dataList.size()) return false;
        if (dataList.contains(t)) return false;
        dataList.add(position, t);
        notifyItemInserted(position);
        return true;
    }

    public boolean addItems(int pos, List<? extends T> datas) {
        if (datas == null) return false;
        if (datas.contains(datas)) return false;
        dataList.addAll(pos, datas);
        notifyItemRangeInserted(pos, datas.size());
        return true;
    }

    public boolean addItems(List<? extends T> datas) {
        if (datas == null) return false;
        if (datas.contains(datas)) return false;
        dataList.addAll(datas);
        notifyItemRangeInserted(getItemCount() - datas.size() >= 0 ? getItemCount() - datas.size() : 0, datas.size());
        return true;
    }

    public boolean addItem(T t) {
        if (t == null) return false;
        if (dataList.contains(t)) return false;
        boolean b = dataList.add(t);
        notifyItemInserted(dataList.size() - 1);
        return b;
    }


    public boolean updateItem(int position) {
        if (position < 0 || position >= dataList.size()) return false;
        notifyItemChanged(position);
        return true;
    }

    public boolean updateItem(T t) {
        if (t == null) return false;
        int index = dataList.indexOf(t);
        if (index >= 0) {
            dataList.set(index, t);
            notifyItemChanged(index);
            return true;
        }
        return false;
    }

    public boolean updateItem(int position, T t) {
        if (position < 0 || position >= dataList.size()) return false;
        if (t == null) return false;
        dataList.set(position, t);
        notifyItemChanged(position);
        return true;
    }

    public boolean removeItem(int position) {
        if (position < 0 || position >= dataList.size()) return false;
        dataList.remove(position);
        notifyItemRemoved(position);
        return true;
    }

    public boolean removeItem(T t) {
        if (t == null) return false;
        int index = dataList.indexOf(t);
        if (index >= 0) {
            dataList.remove(index);
            notifyItemRemoved(index);
            return true;
        }
        return false;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        onBindHoder(holder, getData(position), position);
    }

    public abstract void onBindHoder(ViewHolder holder, T t, int position);

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


}


带header footer 的recyclerView adapter:

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

/**
 * @author xuanyouwu
 * @email [email protected]
 * @time 2016-03-31 14:27
 */
public abstract class HFRecyclerAdapter<H, T, F> extends BaseRecyclerAdapter {
    private static final int HEADER_VIEW_TYPE = -2016;
    private static final int FOOTER_VIEW_TYPE = -2015;

    private final List<T> dataList = new ArrayList<T>();
    private final List<H> headerDataList = new ArrayList<H>();
    private final List<F> footerDataList = new ArrayList<F>();

    public List<T> getDataList() {
        return dataList;
    }

    public List<H> getHeaderDataList() {
        return headerDataList;
    }

    public List<F> getFooterDataList() {
        return footerDataList;
    }

    public boolean isHeader(int pos) {
        if (pos >= 0 && pos < getmHeaderCount())
            return true;
        return false;
    }

    public boolean isFooter(int pos) {
        if (pos >= getItemCount() - getmFooterCount() && pos < getItemCount())
            return true;
        return false;
    }

    public boolean isItem(int pos) {
        if (pos >= getmHeaderCount() && pos < getItemCount() - getmFooterCount())
            return true;
        return false;
    }

    @Override
    public T getItem(int position) {
        if (isItem(position)) {
            int realPos = position - getmHeaderCount();
            if (realPos >= 0 && realPos < dataList.size())
                return dataList.get(realPos);
        }
        return null;
    }

    public H getHeaderItem(int pos) {
        if (isHeader(pos)) {
            if (pos >= 0 && pos < headerDataList.size())
                return headerDataList.get(pos);
        }
        return null;
    }

    public F getFooterItem(int pos) {
        if (isFooter(pos)) {
            int realPos = getItemCount() - 1 - pos;
            if (realPos >= 0 && realPos < footerDataList.size())
                return footerDataList.get(realPos);
        }
        return null;
    }

    public boolean bindHeader(List<H> datas, boolean updateUI) {
        if (datas == null) return false;
        if (getHeaderDataList().containsAll(datas)) return false;
        getHeaderDataList().clear();
        boolean addSuc = getHeaderDataList().addAll(datas);
        if (updateUI) {
            this.notifyDataSetChanged();
        }
        return addSuc;
    }

    public boolean bindFooter(List<F> datas) {
        if (datas == null) return false;
        if (getFooterDataList().containsAll(datas)) return false;
        getFooterDataList().clear();
        boolean addSuc = getFooterDataList().addAll(datas);
        this.notifyItemRangeChanged(getItemCount() - getmFooterCount(), getItemCount());
        return addSuc;
    }

    public boolean bindData(boolean isRefresh, List<T> datas) {
        if (datas == null) return false;
        if (isRefresh) {
            getDataList().clear();
        }
        boolean addSuc = getDataList().addAll(datas);
        this.notifyDataSetChanged();
        return addSuc;
    }

    public boolean addItem(T t) {
        if (t == null) return false;
        if (getDataList().contains(t)) return false;
        boolean add = getDataList().add(t);
        this.notifyItemInserted(getmItemCount() + getmHeaderCount() - 1);
        return add;
    }

    public boolean addItem(T t, int pos) {
        if (t == null) return false;
        if (pos < getmHeaderCount() || pos > getmItemCount() + getmHeaderCount()) return false;
        if (getDataList().contains(t)) return false;
        getDataList().add(pos - getmHeaderCount(), t);
        this.notifyItemInserted(pos);
        return true;
    }


    public void updateItem(int position) {
        this.notifyItemChanged(position);
    }

    public boolean removeItem(int position) {
        if (isItem(position)) {
            int realPos = position - getmHeaderCount();
            getDataList().remove(realPos);
            this.notifyItemRemoved(position);
            return true;
        } else if (isHeader(position)) {
            getHeaderDataList().remove(position);
            this.notifyItemRemoved(position);
            return true;
        } else if (isFooter(position)) {
            getFooterDataList().remove(position - getmHeaderCount() - getmItemCount());
            this.notifyItemRemoved(position);
            return true;
        }
        return false;
    }

    @Override
    public final int getItemViewType(int position) {
        if (isHeader(position)) {
            return getmHeaderViewType();
        } else if (isFooter(position)) {
            return getmFooterViewType();
        } else {
            return getmItemViewType(position);
        }
    }


    /**
     * 中间条目的viewtype
     *
     * @param pos
     * @return
     */
    public int getmItemViewType(int pos) {
        return 0;
    }

    /**
     * header item的viewType
     *
     * @return
     */
    public final int getmHeaderViewType() {
        return HEADER_VIEW_TYPE;
    }

    /**
     * footer item的viewType
     *
     * @return
     */
    public final int getmFooterViewType() {
        return FOOTER_VIEW_TYPE;
    }

    @Override
    public final int bindView(int viewtype) {
        if (viewtype == getmHeaderViewType()) {
            return bindHeaderView();
        } else if (viewtype == getmFooterViewType()) {
            return bindFooterView();
        } else {
            return bindItemView(viewtype);
        }
    }

    /**
     * bind center view
     *
     * @param itemViewType
     * @return
     */
    public abstract int bindItemView(int itemViewType);

    /**
     * bind header view
     *
     * @return
     */
    public abstract int bindHeaderView();

    /**
     * bind footer view
     *
     * @return
     */
    public abstract int bindFooterView();

    @Override
    public final void onBindViewHolder(ViewHolder holder, int position) {
        if (isHeader(position)) {
            onBindHeaderHolder(holder, getHeaderItem(position), position);
        } else if (isFooter(position)) {
            onBindFooterHolder(holder, getFooterItem(position), position);
        } else {
            onBindItemHolder(holder, getItem(position), position);
        }
    }

    /**
     * bind header
     *
     * @param holder
     * @param h
     * @param position
     */
    public abstract void onBindHeaderHolder(ViewHolder holder, H h, int position);

    /**
     * bind center
     *
     * @param holder
     * @param t
     * @param position
     */
    public abstract void onBindItemHolder(ViewHolder holder, T t, int position);

    /**
     * bind footer
     *
     * @param holder
     * @param f
     * @param position
     */
    public abstract void onBindFooterHolder(ViewHolder holder, F f, int position);

    @Override
    public final int getItemCount() {
        return getmHeaderCount() + getmItemCount() + getmFooterCount();
    }

    /**
     * header 条目数量
     *
     * @return
     */
    public int getmItemCount() {
        return dataList.size();
    }

    /**
     * 中间 条目数量
     *
     * @return
     */
    public int getmHeaderCount() {
        return headerDataList.size();
    }

    /**
     * footer 条目数量
     *
     * @return
     */
    public int getmFooterCount() {
        return footerDataList.size();
    }
}


简化版的header adapter:

public abstract class HeaderRecyclerAdapter<H, T, F> extends HFRecyclerAdapter<H, T, F> {

    @Override
    public int bindFooterView() {
        return 0;
    }

    @Override
    public void onBindFooterHolder(ViewHolder holder, F f, int position) {

    }
}


使用实例:

RecyclerView Adapter 封装_第1张图片



itemClick 和局部点击事件:

RecyclerView Adapter 封装_第2张图片




你可能感兴趣的:(RecyclerView Adapter 封装)