最近的項(xiàng)目,使用RecyclerView比較多恢暖,導(dǎo)致需要寫大量的Adapter和ViewHolder排监。不想偷懶的程序員不是好程序員。于是杰捂,查了一些資料舆床,對(duì)RecyclerView的Adapter進(jìn)行了一些封裝。使用的時(shí)候減少了大量的工作嫁佳。
先來點(diǎn)效果圖
封裝后的Adapter使用:
class DragSwipeAdapter extends BaseAdapter<String> {
public DragSwipeAdapter(Context mContext, List<String> mDatas, int mLayoutId) {
super(mContext, mDatas, mLayoutId);
}
@Override
protected void convert(Context mContext, BaseViewHolder holder, String s) {
holder.setText(R.id.tv,s);
}
}
只要傳入數(shù)據(jù)源和item布局即可挨队,是不是超級(jí)簡單。
下面看看怎么實(shí)現(xiàn)的吧蒿往。
通用ViewHolder
package org.raphets.demorecyclerview.adapter;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;
public class BaseViewHolder extends RecyclerView.ViewHolder {
SparseArray<View> mViews;
View mItemView;
public BaseViewHolder(View itemView) {
super(itemView);
mItemView = itemView;
mViews = new SparseArray<>();
}
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mItemView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public BaseViewHolder setText(int viewId, int resId) {
TextView textView = getView(viewId);
textView.setText(resId);
return this;
}
public BaseViewHolder setText(int viewId, String text) {
TextView textView = getView(viewId);
textView.setText(text);
return this;
}
public BaseViewHolder setText(int viewId, SpannableStringBuilder text) {
TextView textView = getView(viewId);
textView.setText(text);
return this;
}
public BaseViewHolder setImageResource(int viewId, int resId) {
ImageView view = getView(viewId);
view.setImageResource(resId);
return this;
}
public BaseViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
ImageView view = getView(viewId);
view.setImageBitmap(bitmap);
return this;
}
public BaseViewHolder setImageDrawable(int viewId, Drawable drawable) {
ImageView view = getView(viewId);
view.setImageDrawable(drawable);
return this;
}
public BaseViewHolder setBackgroundColor(int viewId, int color) {
View view = getView(viewId);
view.setBackgroundColor(color);
return this;
}
public BaseViewHolder setBackgroundResource(int viewId, int backgroundRes) {
View view = getView(viewId);
view.setBackgroundResource(backgroundRes);
return this;
}
public BaseViewHolder setTextColor(int viewId, int textColor) {
TextView view = getView(viewId);
view.setTextColor(textColor);
return this;
}
@SuppressLint("NewApi")
public BaseViewHolder setAlpha(int viewId, float value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getView(viewId).setAlpha(value);
} else {
// Pre-honeycomb hack to set Alpha value
AlphaAnimation alpha = new AlphaAnimation(value, value);
alpha.setDuration(0);
alpha.setFillAfter(true);
getView(viewId).startAnimation(alpha);
}
return this;
}
public BaseViewHolder setVisible(int viewId, boolean visible) {
View view = getView(viewId);
view.setVisibility(visible ? View.VISIBLE : View.GONE);
return this;
}
public BaseViewHolder setTag(int viewId, Object tag) {
View view = getView(viewId);
view.setTag(tag);
return this;
}
public BaseViewHolder setTag(int viewId, int key, Object tag) {
View view = getView(viewId);
view.setTag(key, tag);
return this;
}
public BaseViewHolder setChecked(int viewId, boolean checked) {
Checkable view = (Checkable) getView(viewId);
view.setChecked(checked);
return this;
}
/**
* 關(guān)于事件監(jiān)聽
*/
public BaseViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {
View view = getView(viewId);
view.setOnClickListener(listener);
return this;
}
public BaseViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) {
View view = getView(viewId);
view.setOnTouchListener(listener);
return this;
}
public BaseViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) {
View view = getView(viewId);
view.setOnLongClickListener(listener);
return this;
}
}
這里對(duì)TextView瞒瘸、ImageView等常用控件的一些常用方法進(jìn)行了封裝。有了這個(gè)通用ViewHolder熄浓,再多的Adapter也只需要這一個(gè)ViewHolder情臭,不用每寫一個(gè)Adapter就要寫一個(gè)ViewHolder。
<br />
通用Adapter
package org.raphets.demorecyclerview.adapter;
import android.content.Context;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by RaphetS on 2016/9/28.
* 普通的萬能Adapter
* 支持onItemClick
* 支持onLongItemClick
*/
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
private Context mContext;
private List<T> mDatas;
private int mLayoutId;
private OnItemClickListener mItemClickListener;
private onLongItemClickListener mLongItemClickListener;
public BaseAdapter(Context mContext, List<T> mDatas, int mLayoutId) {
this.mContext = mContext;
this.mDatas = mDatas;
this.mLayoutId = mLayoutId;
}
public void updateData(List<T> data) {
mDatas.clear();
mDatas.addAll(data);
notifyDataSetChanged();
}
public void addAll(List<T> data) {
mDatas.addAll(data);
notifyDataSetChanged();
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);
BaseViewHolder holder = new BaseViewHolder(view);
return holder;
}
@Override
public int getItemCount() {
return mDatas.size();
}
@Override
public void onBindViewHolder(BaseViewHolder holder, final int position) {
convert(mContext, holder, mDatas.get(position));
if (mItemClickListener != null) {
holder.mItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mItemClickListener.onItemClick(v, position);
}
});
}
if (mLongItemClickListener != null) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mLongItemClickListener.onLongItemClick(v, position);
return true;
}
});
}
}
protected abstract void convert(Context mContext, BaseViewHolder holder, T t);
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public interface onLongItemClickListener {
void onLongItemClick(View view, int postion);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mItemClickListener = listener;
}
public void setonLongItemClickListener(onLongItemClickListener listener) {
this.mLongItemClickListener = listener;
}
}
這里對(duì)RecyclerViewAdapter通用的地方封裝了一下赌蔑,然后封裝了一下onItemClick和onLongItemClick事件俯在。
<br />
如何使用
首先,寫一個(gè)Adapter繼承通用Adapter
class DragSwipeAdapter extends BaseAdapter<String> {
public DragSwipeAdapter(Context mContext, List<String> mDatas, int mLayoutId) {
super(mContext, mDatas, mLayoutId);
}
@Override
protected void convert(Context mContext, BaseViewHolder holder, String s) {
holder.setText(R.id.tv,s)
.setTextColor(R.id.tv, Color.RED)
.setImageResource(R.id.imageView,R.mipmap.ic_launcher);
ImageView imageView=holder.getView(R.id.imageView);
imageView.setImageResource(R.mipmap.ic_launcher);
}
}
代碼比起原生的是不是少很多娃惯?
然后在Activity中使用即可跷乐。只要傳入Context,數(shù)據(jù)源趾浅,item布局即可愕提。
mAdapter = new DragSwipeAdapter(this, mData, R.layout.item);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter);
是不是少了大量的工作。
<br />
但是貌似封裝的還不夠皿哨,我們的RecyclerView經(jīng)常需要使用的功能還有下拉刷新浅侨,上拉加載,加個(gè)headview等等证膨。下拉刷新好說如输,直接使用v4包里的SwipeRefreshLayout即可,上拉加載和HeadView怎么辦呢央勒?
<br />
下面我對(duì)上拉加載和HeadView也進(jìn)行了一下封裝不见。
(目前只支持LinearLayoutManager布局的RecyclerView)
-
首先ViewHoder不變,依然是上面的ViewHolder崔步。
-
然后寫了一個(gè)底部加載中的一個(gè)item布局稳吮。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:gravity="center"
android:padding="20dp"
android:layout_height="wrap_content">
<ProgressBar
style="@android:style/Widget.ProgressBar.Small"
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="10dp"
android:text="正在加載..."
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
-
下面是封裝的Adapter
package org.raphets.demorecyclerview.adapter;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.raphets.demorecyclerview.R;
import java.util.List;
/**
* Created by RaphetS on 2016/10/1.
* 支持上拉加載
* 支持添加HeadView
* 底部有進(jìn)度條
*/
public abstract class BaseLoadMoreHeaderAdapter<T> extends RecyclerView.Adapter {
private Context mContext;
private boolean isLoading=false;
private OnLoadMoreListener mOnLoadMoreListener;
private OnItemClickListener mItemClickListener;
private onLongItemClickListener mLongItemClickListener;
private List<T> mDatas;
private int mLayoutId;
private View mHeadView;
private final static int TYPE_HEADVIEW=100;
private final static int TYPE_ITEM=101;
private final static int TYPE_PROGRESS=102;
public BaseLoadMoreHeaderAdapter(Context mContext, RecyclerView recyclerView, List<T> mDatas, int mLayoutId) {
this.mContext = mContext;
this.mDatas = mDatas;
this.mLayoutId = mLayoutId;
init(recyclerView);
}
private void init(RecyclerView recyclerView) {
//mRecyclerView添加滑動(dòng)事件監(jiān)聽
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int totalItemCount = linearLayoutManager.getItemCount();
int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading &&dy>0&&lastVisibleItemPosition>=totalItemCount-1) {
//此時(shí)是刷新狀態(tài)
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
});
}
public void updateData(List<T> data) {
mDatas.clear();
mDatas.addAll(data);
notifyDataSetChanged();
}
public void addAll(List<T> data) {
mDatas.addAll(data);
notifyDataSetChanged();
}
public void addHeadView(View headView){
mHeadView=headView;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType==TYPE_ITEM){
View itemView= LayoutInflater.from(mContext).inflate(mLayoutId,parent,false);
BaseViewHolder baseViewHolder=new BaseViewHolder(itemView);
return baseViewHolder;
}else if (viewType==TYPE_HEADVIEW){
HeadViewHolder headViewHolder=new HeadViewHolder(mHeadView);
return headViewHolder;
} else{
View progressView=LayoutInflater.from(mContext).inflate(R.layout.progress_item,parent,false);
ProgressViewHolder progressViewHolder= new ProgressViewHolder(progressView);
return progressViewHolder;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof BaseViewHolder){
convert(mContext, holder, mDatas.get(position));
((BaseViewHolder) holder).mItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mItemClickListener.onItemClick(v,position-1);
}
});
((BaseViewHolder) holder).mItemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mLongItemClickListener.onLongItemClick(v,position-1);
return true;
}
});
}
}
@Override
public int getItemViewType(int position) {
if (mHeadView!=null){
if (position==getItemCount()-1){
return TYPE_PROGRESS;
}else if (position==0){
return TYPE_HEADVIEW;
}else {
return TYPE_ITEM;
}
}else {
if (position==getItemCount()-1){
return TYPE_PROGRESS;
}else {
return TYPE_ITEM;
}
}
}
public abstract void convert(Context mContext, RecyclerView.ViewHolder holder, T t);
@Override
public int getItemCount() {
return mDatas.size()+1;
}
public void setLoading(boolean b){
isLoading=b;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public interface onLongItemClickListener {
void onLongItemClick(View view, int postion);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mItemClickListener = listener;
}
public void setonLongItemClickListener(onLongItemClickListener listener) {
this.mLongItemClickListener = listener;
}
public void setOnLoadMoreListener(OnLoadMoreListener listener){
this.mOnLoadMoreListener=listener;
}
public interface OnLoadMoreListener{
void onLoadMore();
}
public class ProgressViewHolder extends RecyclerView.ViewHolder{
public ProgressViewHolder(View itemView) {
super(itemView);
}
}
public class HeadViewHolder extends RecyclerView.ViewHolder{
public HeadViewHolder(View itemView) {
super(itemView);
}
}
}
上拉加載和HeadView都是通過ItemViewType實(shí)現(xiàn)的,通過不同的position得到不同的ItemViewType井濒,然后在onCreateViewHolder()的時(shí)候通過不同的ItemViewType加載不同的布局灶似,創(chuàng)建不同的ViewHolder慎陵。
-
使用實(shí)例
1、首先寫一個(gè)Adapter繼承BaseLoadMoreHeaderAdapter喻奥,注意要判斷holder的類型
class MyAdapter extends BaseLoadMoreHeaderAdapter<String> {
public MyAdapter(Context mContext, RecyclerView recyclerView, List<String> mDatas, int mLayoutId) {
super(mContext, recyclerView, mDatas, mLayoutId);
}
@Override
public void convert(Context mContext, RecyclerView.ViewHolder holder, String s) {
if (holder instanceof BaseViewHolder){
((BaseViewHolder) holder).setText(R.id.tv,s);
}
}
}
2席纽、在Activity中設(shè)置Adapter
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
mAdapter=new MyAdapter(this,mRecyclerView,mDatas,R.layout.item);
mRecyclerView.setLayoutManager(layoutManager);
View headView= LayoutInflater.from(this).inflate(R.layout.headview,mRecyclerView,false);
mAdapter.addHeadView(headView);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
@Override
public void onLoadMore() {
loadMore();
}
});
mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_SHORT).show();
}
});
注意這里,需要先給RecyclerView設(shè)置LayoutManager撞蚕,在加載HeadView(View headView= LayoutInflater.from(this).inflate(R.layout.headview,mRecyclerView,false);)
否則會(huì)報(bào)錯(cuò)润梯。
<br />