前言
ListView、RecycleView、GridView是我们开发时最常用的控件,通过打造一款通用的Adapter可以方便开发。这里的RecycleView是分开的
ListView、GridView的Adapter的打造
直接上代码:SimpleBaseAdapter
public abstract class SimpleBaseAdapter<T> extends BaseAdapter {
public Context mContext;
protected List<T> mList;
private LayoutInflater mInflater;
public SimpleBaseAdapter(Context context) {
init(context);
}
public SimpleBaseAdapter(Context context, List<T> list) {
mList = list;
init(context);
}
private void init(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
MyHolder holder=null;
if (convertView==null){
convertView=LayoutInflater.from(mContext).inflate(getLayoutID(),parent,false);
holder=new MyHolder(mContext,convertView);
convertView.setTag(getLayoutID(),holder);
}else {
holder= (QuickHolder) convertView.getTag(getLayoutID());
}
convert(holder,mList.get(position));
holder.convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener!=null){
mOnItemClickListener.onItemClick(v,position,mList.get(position));
}
}
});
return convertView;
}
protected abstract int getLayoutID();
protected abstract void convert(MyHolder holder, T t);
public List<T> getList() {
return mList;
}
public void setList(List<T> mList) {
this.mList = mList;
notifyDataSetChanged();
}
public void addList(List<T> nList) {
if (null == mList) {
mList = new ArrayList<T>();
}
mList.addAll(nList);
notifyDataSetChanged();
}
protected LayoutInflater getInflater() {
return mInflater;
}
@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}
@Override
public Object getItem(int position) {
return mList == null ? null : mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public OnItemClickListener<T> mOnItemClickListener;
public interface OnItemClickListener<T> {
void onItemClick(View view, int position, T model);
void onItemLongClick(View view, int position, T model);
}
public void setOnItemClickListener(OnItemClickListener<T> listener) {
this.mOnItemClickListener = listener;
}
public class MyHolder MyHolder extends BaseHolder{
protected QuickHolder(Context context, View view) {
super(context,view);
}
}
}
这里继承BaseAdapter,通过对getView()方法的将convertView与Holder进行关联,并提供点击。同时提供BaseHolder代码:
public class BaseHolder {
private final SparseArray<View> views;
private final Context context;
public View convertView;
protected BaseHolder(Context context, View view) {
this.context = context;
this.views = new SparseArray<View>();
convertView = view;
}
public BaseHolder setText(int viewId, CharSequence value) {
TextView view = getView(viewId);
view.setText(value);
return this;
}
public BaseHolder setImageResource(int viewId, int imageResId) {
ImageView view = getView(viewId);
view.setImageResource(imageResId);
return this;
}
public BaseHolder setBackgroundColor(int viewId, int color) {
View view = getView(viewId);
view.setBackgroundColor(color);
return this;
}
public BaseHolder setBackgroundRes(int viewId, int backgroundRes) {
View view = getView(viewId);
view.setBackgroundResource(backgroundRes);
return this;
}
public BaseHolder setTextColor(int viewId, int textColor) {
TextView view = getView(viewId);
view.setTextColor(textColor);
return this;
}
public BaseHolder setTextColorRes(int viewId, int textColorRes) {
TextView view = getView(viewId);
view.setTextColor(context.getResources().getColor(textColorRes));
return this;
}
public BaseHolder setImageDrawable(int viewId, Drawable drawable) {
ImageView view = getView(viewId);
view.setImageDrawable(drawable);
return this;
}
public BaseHolder setImageUrl(int viewId, String imageUrl) {
ImageView view = getView(viewId);
Glide.with(context).load(imageUrl).crossFade().into(view);
return this;
}
public BaseHolder setImageBitmap(int viewId, Bitmap bitmap) {
ImageView view = getView(viewId);
view.setImageBitmap(bitmap);
return this;
}
public BaseHolder setAlpha(int viewId, float value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getView(viewId).setAlpha(value);
} else {
AlphaAnimation alpha = new AlphaAnimation(value, value);
alpha.setDuration(0);
alpha.setFillAfter(true);
getView(viewId).startAnimation(alpha);
}
return this;
}
public BaseHolder setVisible(int viewId, boolean visible) {
View view = getView(viewId);
view.setVisibility(visible ? View.VISIBLE : View.GONE);
return this;
}
public BaseHolder setProgress(int viewId, int progress) {
ProgressBar view = getView(viewId);
view.setProgress(progress);
return this;
}
public BaseHolder setOnClickListener(int viewId, View.OnClickListener listener) {
View view = getView(viewId);
view.setOnClickListener(listener);
return this;
}
public BaseHolder setOnTouchListener(int viewId, View.OnTouchListener listener) {
View view = getView(viewId);
view.setOnTouchListener(listener);
return this;
}
public BaseHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) {
View view = getView(viewId);
view.setOnLongClickListener(listener);
return this;
}
public BaseHolder setOnItemClickListener(int viewId, AdapterView.OnItemClickListener listener) {
AdapterView view = getView(viewId);
view.setOnItemClickListener(listener);
return this;
}
public BaseHolder setOnItemLongClickListener(int viewId, AdapterView.OnItemLongClickListener listener) {
AdapterView view = getView(viewId);
view.setOnItemLongClickListener(listener);
return this;
}
public BaseHolder setOnItemSelectedClickListener(int viewId, AdapterView.OnItemSelectedListener listener) {
AdapterView view = getView(viewId);
view.setOnItemSelectedListener(listener);
return this;
}
public BaseHolder setOnCheckedChangeListener(int viewId, CompoundButton.OnCheckedChangeListener listener) {
CompoundButton view = getView(viewId);
view.setOnCheckedChangeListener(listener);
return this;
}
public BaseHolder setTag(int viewId, Object tag) {
View view = getView(viewId);
view.setTag(tag);
return this;
}
public BaseHolder setTag(int viewId, int key, Object tag) {
View view = getView(viewId);
view.setTag(key, tag);
return this;
}
public BaseHolder setChecked(int viewId, boolean checked) {
View view = getView(viewId);
if (view instanceof CompoundButton) {
((CompoundButton) view).setChecked(checked);
} else if (view instanceof CheckedTextView) {
((CheckedTextView) view).setChecked(checked);
}
return this;
}
public BaseHolder setAdapter(int viewId, Adapter adapter) {
AdapterView view = getView(viewId);
view.setAdapter(adapter);
return this;
}
@SuppressWarnings("unchecked")
public <T extends View> T getView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = convertView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
}
BaseHolder提供了一系列的方法供我们显示数据,在继承BaseAdapter后需要实现 int getLayoutID(); convert(MyHolder holder, T t);这两个方法,这时候就可以直接使用(例如:holder.setText(R.id.text1,t.getName()))
RecycleView Adapter的打造
public abstract class BaseRecyclerAdapter<T> extends
RecyclerView.Adapter<BaseRecyclerViewHolder> {
protected Context mContext;
protected LayoutInflater mInflater;
protected List<T> mDatas = new ArrayList<>();
public OnItemClickListener<T> mOnItemClickListener;
public BaseRecyclerAdapter(Context context, List<T> datas) {
mContext = context;
mInflater = LayoutInflater.from(context);
if (datas != null && datas.size()>0) {
mDatas = datas;
}
}
protected abstract int getLayoutId();
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
int count = 0;
if (mDatas.size() > 0) {
count = mDatas.size();
}
return count;
}
public void remove(int position) {
mDatas.remove(position);
}
public void removeAll() {
mDatas.clear();
}
@Override
public long getItemId(int position) {
return position;
}
public List<T> getDatas() {
return mDatas;
}
// 点击事件接口
public interface OnItemClickListener<T> {
void onItemClick(View view, int position, T model);
void onItemLongClick(View view, int position, T model);
}
public void setOnItemClickListener(OnItemClickListener<T> listener) {
this.mOnItemClickListener = listener;
}
@Override
public void onBindViewHolder(BaseRecyclerViewHolder holder, final int position) {
convert(holder,mDatas.get(position));
holder.convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener!=null){
mOnItemClickListener.onItemClick(v,position,mDatas.get(position));
}
}
});
}
protected abstract void convert(BaseRecyclerViewHolder holder, T t);
@Override
public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false);
return new QuickHolder(parent.getContext(),view);
}
public class QuickHolder extends BaseRecyclerViewHolder{
protected QuickHolder(Context context, View view) {
super(context, view);
}
}
BaseRecycleViewHolder与前面的BaseHolder一样,只是需要继承RecyclerView.ViewHolder