一、Adapter的封装
由于我们的RecycleView在项目中很常用,因此有必要对Adapter进行封装。
封装的原则是:
BaseAdapter的代码如下:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Adapter的简单封装
*/
public abstract class BaseAdapter extends RecyclerView.Adapter {
protected static final String TAG = BaseAdapter.class.getSimpleName();
protected final Context context;
protected final int layoutResId;
protected List datas;
private OnItemClickListener mOnItemClickListener = null;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public BaseAdapter(Context context, int layoutResId) {
this(context, layoutResId, null);
}
public BaseAdapter(Context context, int layoutResId, List datas) {
this.datas = datas == null ? new ArrayList() : datas;
this.context = context;
this.layoutResId = layoutResId;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutResId, viewGroup, false);
BaseViewHolder vh = new BaseViewHolder(view, mOnItemClickListener);
return vh;
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
T item = getItem(position);
convert((H) holder, item);
}
@Override
public int getItemCount() {
if (datas == null || datas.size() <= 0)
return 0;
return datas.size();
}
public T getItem(int position) {
if (position >= datas.size()) return null;
return datas.get(position);
}
/**
* 清空数据
*/
public void clearData() {
int size = datas.size();
datas.clear();
notifyItemRangeRemoved(0, size);
}
/**
* 下拉刷新,清除原有数据,添加新数据
*
* @param newData
*/
public void refreshData(List newData) {
datas.clear();
datas.addAll(newData);
notifyItemRangeChanged(0, datas.size());
}
/**
* 在原来数据的末尾追加新数据
*
* @param moreData
*/
public void loadMoreData(List moreData) {
int lastPosition = datas.size();
datas.addAll(lastPosition, moreData);
notifyItemRangeInserted(lastPosition, moreData.size());
}
/**
* Implement this method and use the helper to adapt the view to the given item.
*
* @param viewHoder A fully initialized helper.
* @param item The item that needs to be displayed.
*/
protected abstract void convert(H viewHoder, T item);
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
}
BaseViewHolder的代码如下:
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 封装的ViewHolder
*/
public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//使用数组把条目中的View保存起来
private SparseArray views;
private BaseAdapter.OnItemClickListener mOnItemClickListener;
public BaseViewHolder(View itemView, BaseAdapter.OnItemClickListener onItemClickListener) {
super(itemView);
itemView.setOnClickListener(this);
this.mOnItemClickListener = onItemClickListener;
this.views = new SparseArray();
}
public TextView getTextView(int viewId) {
return retrieveView(viewId);
}
public Button getButton(int viewId) {
return retrieveView(viewId);
}
public ImageView getImageView(int viewId) {
return retrieveView(viewId);
}
public View getView(int viewId) {
return retrieveView(viewId);
}
protected T retrieveView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = itemView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, getLayoutPosition());
}
}
}
为了使用方便,再提供一个SimpleAdapter:
import android.content.Context;
import java.util.List;
/**
* 一个简易的adapter
*/
public abstract class SimpleAdapter extends BaseAdapter {
public SimpleAdapter(Context context, int layoutResId) {
super(context, layoutResId);
}
public SimpleAdapter(Context context, int layoutResId, List datas) {
super(context, layoutResId, datas);
}
}
二、封装Adapter的基本使用
1、使用
mAdapter = new SimpleAdapter(getActivity(), R.layout.item_hot_wares) {
@Override
protected void convert(BaseViewHolder holder, final Page.Ware item) {
SimpleDraweeView s = (SimpleDraweeView) holder.getView(R.id.drawee_view);
s.setImageURI(Uri.parse(item.getImgUrl()));
holder.getTextView(R.id.text_title).setText(item.getName());
holder.getTextView(R.id.text_price).setText(item.getPrice() + "");
holder.getButton(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), item.getName(), Toast.LENGTH_SHORT).show();
}
});
}
};
2、还可以进行点击监听
mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
}
});
如果觉得我的文字对你有所帮助的话,欢迎关注我的公众号:
我的群欢迎大家进来探讨各种技术与非技术的话题,有兴趣的朋友们加我私人微信huannan88,我拉你进群交(♂)流(♀)。