依旧模仿别人造轮子,之前也写过一篇打造一个通用的Adapter,不过那个是继承自
BaseAdapter
的,不能用于RecyclerView.因为RecyclerView
的ViewHolder
本来就有比较好的封装,所以也不怎么封装.
package com.example.august.androidnew;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by August on 16/5/28.
*/
public abstract class GenericRecyclerAdapter<T> extends RecyclerView.Adapter<GenericRecyclerAdapter.ViewHolder> {
private List<T> mDatas;
private int layoutID;
private OnItemClickListener mOnItemClickListener;
public GenericRecyclerAdapter(List<T> mDatas, int layoutID) {
this.mDatas = mDatas;
this.layoutID = layoutID;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
@Override
public GenericRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(layoutID, parent, false);
return new GenericRecyclerAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final GenericRecyclerAdapter.ViewHolder holder, final int position) {
if (mOnItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnItemClickListener.onItemClick(holder.itemView, position);
}
});
}
setData(holder, mDatas, position);
}
public abstract void setData(ViewHolder holder, List<T> mDatas, int position);
@Override
public int getItemCount() {
return mDatas.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> views;
public ViewHolder(View itemView) {
super(itemView);
}
public View getView(int viewID) {
View view = views.get(viewID);
if (view == null) {
view = itemView.findViewById(viewID);
views.put(viewID, view);
}
return view;
}
public void setText(int viewID, String text) {
View view = getView(viewID);
if (view instanceof TextView) {
((TextView) view).setText(text);
}
}
public void setBitmapImage(int viewID, Bitmap bitmap) {
View view = getView(viewID);
if (view instanceof ImageView) {
((ImageView) view).setImageBitmap(bitmap);
}
}
public void setRescouceImage(int viewID, int resID) {
View view = getView(viewID);
if (view instanceof ImageView) {
((ImageView) view).setImageResource(resID);
}
}
public void setOnClickListener(int viewID, View.OnClickListener listener) {
View view = getView(viewID);
if (view != null) {
view.setOnClickListener(listener);
}
}
public void setOnLongClickListener(int viewID, View.OnLongClickListener listener) {
View view = getView(viewID);
if (view != null) {
view.setOnLongClickListener(listener);
}
}
}
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
}