万能RecyclerView 的BaseAdapter

最近公司开始使用RecyclerView,研究了一下,一看什么都得需要自定义,不过使用起来确实强大,自定义方便。但是不想ListView有addHeaderView(),addFooterView()等。下面分享一下我自己写的RecyclerView 的Adapter。已经实现了添加头布局的功能。

1、BaseAdapter,这个Adapter只是一个基类,实现具体的adapter需要继承这个类。

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    protected static final String TAG = BaseAdapter.class.getSimpleName();

    protected final Context context;
    protected List<T> datas = null;

    private int itemLayoutResId;//item布局
    private int footerLayoutResId;
    private int emptyLayoutResId;

    private boolean hasData;
    private boolean hasMoreData;
    private boolean hasFooter;

    private ArrayList<View> emptys = new ArrayList<>();
    private ArrayList<View> headers = new ArrayList<>();

    private final int TYPE_HEADER = 1;
    private final int TYPE_FOOTER = 2;
    private final int TYPE_EMPTY = 3;
    private final int TYPE_ITEM = 4;

    public BaseAdapter(Context context, int itemLayoutResId) {
        this(context, itemLayoutResId, null);
    }

    public BaseAdapter(Context context, int itemLayoutResId, int emptyLayoutResId) {
        this(context, itemLayoutResId, null);
        this.emptyLayoutResId = emptyLayoutResId;
    }

    public BaseAdapter(Context context, int itemLayoutResId, int emptyLayoutResId, int footerLayoutResId) {
        this(context, itemLayoutResId, null);
        this.emptyLayoutResId = emptyLayoutResId;
        this.footerLayoutResId = footerLayoutResId;
    }

    public BaseAdapter(Context context, int itemLayoutResId, List<T> datas) {
        this.context = context;
        this.itemLayoutResId = itemLayoutResId;
        this.datas = datas == null ? new ArrayList<T>() : datas;
    }

    public boolean isHasData() {
        return hasData;
    }

    public void setIsHasData(boolean isHasData) {
        this.hasData = isHasData;
    }

    public boolean isHasFooter() {
        return hasFooter;
    }

    public void setHasFooter(boolean hasFooter) {
        if (this.hasFooter != hasFooter) {
            this.hasFooter = hasFooter;
            notifyDataSetChanged();
        }
    }

    public boolean isHasMoreData() {
        return hasMoreData;
    }

    public void setHasMoreData(boolean hasMoreData) {
        if (this.hasMoreData != hasMoreData) {
            this.hasMoreData = hasMoreData;
            notifyDataSetChanged();
        }
    }

    public void setHasMoreDataAndFooter(boolean hasMoreData, boolean hasFooter) {
        if (this.hasMoreData != hasMoreData || this.hasFooter != hasFooter) {
            this.hasMoreData = hasMoreData;
            this.hasFooter = hasFooter;
            notifyDataSetChanged();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View footerView = LayoutInflater.from(context).inflate(footerLayoutResId, parent, false);
            FooterViewHolder footerViewHolder = new FooterViewHolder(footerView);
            return footerViewHolder;
        } else if (viewType == TYPE_EMPTY) {
            EmptyViewHolder emptyViewHolder = new EmptyViewHolder(emptys.get(0));
            return emptyViewHolder;
        } else if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(itemLayoutResId, parent, false);
            ItemViewHolder itemViewHolder = new ItemViewHolder(view);
            return itemViewHolder;
        } else if (viewType == TYPE_HEADER) {
            View headerView = headers.get(0);
            HeaderViewHolder headerViewHolder = new HeaderViewHolder(headerView);
            return headerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHoder, int position) {
        T item = getItem(position);
        convert(viewHoder, item);
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;
        if (position == 0 && headers.size() > 0) {
            type = TYPE_HEADER;
        } else if (position == getBasicItemCount() && hasFooter) {
            type = TYPE_FOOTER;
        } else if (getBasicItemCount() == 0 && !hasData) {
            type = TYPE_EMPTY;
        } else {
            type = TYPE_ITEM;
        }
        return type;
    }

    @Override
    public int getItemCount() {
        int size = 0;
        if (getBasicItemCount() > 0) {
            hasData = true;
            size = getBasicItemCount() + headers.size() + (hasFooter ? 1 : 0);
        } else if (getBasicItemCount() == 0) {
            hasData = false;
            size = emptys.size();
        }
        return size;
    }

    public int getBasicItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public T getItem(int position) {
        return position >= datas.size() ? null : datas.get(position);
    }

    public void clear() {
        emptys.clear();
        for (Iterator it = datas.iterator(); it.hasNext(); ) {
            T t = (T) it.next();
            int position = datas.indexOf(t);
            it.remove();
            notifyItemRemoved(position);
        }
        hasFooter = false;
        notifyDataSetChanged();
    }

    public void removeItem(T t) {
        int position = datas.indexOf(t);
        datas.remove(position);
        notifyItemRemoved(position);
    }

    public void addData(List<T> datas) {
        addData(0, datas);
    }

    public void addData(int position, List<T> list) {
        if (list != null && list.size() > 0) {
            for (T t : list) {
                datas.add(position, t);
                notifyItemInserted(position);
            }
        }
    }

    public void refreshData(List<T> list) {
        clear();
        if (list != null && list.size() > 0) {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                datas.add(i, list.get(i));
                //notifyItemInserted(i);
            }
        } else {
            View emptyView = View.inflate(context, emptyLayoutResId, null);
            ViewGroup.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            emptyView.setLayoutParams(layoutParams);
            emptys.add(emptyView);
        }
        notifyDataSetChanged();
    }

    public void loadMoreData(List<T> list) {
        if (list != null && list.size() > 0) {
            int size = list.size();
            int begin = datas.size();
            for (int i = 0; i < size; i++) {
                datas.add(list.get(i));
                //notifyItemInserted(i + begin);
            }
            notifyDataSetChanged();
        }
    }

    public void addHeaderView(View view) {
        ViewGroup.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        headers.add(view);
        notifyDataSetChanged();
    }

    protected abstract void convert(RecyclerView.ViewHolder viewHoder, T item);
}

2、FooterViewHolder,我主要是用于上拉加载的时候显示加载更多

public class FooterViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> views;

    public FooterViewHolder(View itemView)
    {
        super(itemView);
        this.views = new SparseArray<View>();
    }

    /** * 根据资源ID获取布局中相应的控件 */
    public View getView(int viewId)
    {
        View view = views.get(viewId);
        if (view == null)
        {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return view;
    }

}

3、EmptyViewHolder

public class EmptyViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> views;

    public EmptyViewHolder(View itemView)
    {
        super(itemView);
        this.views = new SparseArray<View>();
    }

    /** * 根据资源ID获取布局中相应的控件 */
    public View getView(int viewId)
    {
        View view = views.get(viewId);
        if (view == null)
        {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return view;
    }

}

4、ItemViewHolder

public class ItemViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> views;

    public ItemViewHolder(View itemView)
    {
        super(itemView);

        this.views = new SparseArray<View>();
    }

    /** * 根据资源ID获取布局中相应的控件 */
    public View getView(int viewId)
    {
        View view = views.get(viewId);
        if (view == null)
        {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return view;
    }
}

5、

实例讲解:
下面我讲一下这个BaseAdapter怎么使用哈
//使用泛型,指定Adapter的实例类
public class OrderAdapter extends BaseAdapter<Order> {
    public OrderAdapter(Context context)
    {
        //第一个是item布局,第二个是没有数据的时候空布局,第三个是下拉加载更多的时候显示的footerView
        super(context, R.layout.order_item, R.layout.order_none, R.layout.footer_view);
    }
    //重写这个方法为ItemView绑定数据
     @Override
    protected void convert(RecyclerView.ViewHolder mViewHoder, final Order item)
    {

    }
}

你可能感兴趣的:(RecyclerVi)