老规矩还是对最近一段时间做下总结,看到上边的日记更新时间还是4月,虽然并没有很多人去阅读他,可是这并不能代表我就不接着写下去了,我特别喜欢我自己给自己说的一句话,
趁着年轻还能蹦跶的时候赶紧多蹦跶下,蹦跶的时间能有多久就多久,向小强一样的顽强的活着,做一个风吹不倒,雨淋不湿,雷劈不着,上能端红酒,下能进厨房的 合格90后;
最近一直在忙一个公司的项目所以一直感觉抽不开身,只是感觉,可是事实.........估计对自己太放松了,一直以来就想去写几篇关于RecyclerView的文章可是在百度上一搜一大把 ,顿时一点心情都没有,别人已经早就造好轮子了,你还去浪费时间和经历,可是不甘心啊,为啥别人就可以写出这么好的代码,而我就不行了,真的是别人水平比我高很多还是别人比我更加用心了,很多一部分开发者估计心态跟我估计差不多,直接复制copy就好了,依赖一下库,还操那心干嘛,闲的,先把功能实现再说,可是我还是很相信真正的开发者还是想打造一套属于自己的代码,敲完之后和copy之后的感觉真的是不一样的,也许敲完之后才能明白别人写代码的用意,说了这么多对很多人来说觉得是废话 ,我还是我,就像我第一篇写的一样
简书 http://www.jianshu.com/p/97fdd472c86e
不想做一个完全的伸手党;所有我决定这篇文章我必须写出来:
在这里还是分几步去写:采用总分总的形式,这样看起来结构跟加清晰:
在这里我插入一条总的结构图,这个结构图也是我现在要完善的东西,如果有小伙伴想和我一起来造轮子 其中基本上已经完成了一大半,其他的还在完善之中,非常欢迎,毕竟打造一套属于自己的用起来找bug还是快很多啊;哈哈哈
RecyclerView的由来:
说起RecyclerView估计是只要是Android开发者没有一个不熟悉的,安卓5.0之后将之前的列表控件替换,比起之前的ListVie\w 谷歌爸爸 强制使用了ViewHolder来缓存View,比起ListView真的是很用,目前现在的主流app都会有列表结构的,所以这个东西不仅我们要简单的会用,最好是能能用的很好,基本的配置我就不多说了,哈哈 估计大家比我们更加的聪明,配置这么简单的事情对你们so easy;
RecyclerView的组成结构:
说起他不的不提到他的五虎上将
- RecyclerView.LayoutManager 负责Item视图的布局的显示管理
- RecyclerView.ItemDecoration 给每一项Item视图添加子View,例如可以进行画分隔线之类
- RecyclerView.ItemAnimator 负责处理数据添加或者删除时候的动画效果
- RecyclerView.Adapter 为每一项Item创建视图
- RecyclerView.ViewHolder 承载Item视图的子布局
基本上行开发都是围绕这几点去写,五虎上将非同小可:唠唠叨叨终于把上边的写完了,现在终于到了正题,坐直了,睁大眼睛了.嗯嗯嗯;正式封装了
RecyclerView的ViewHolder封装:
- 未封装之前的的代码:(一般模式)
public class ComAdapter extends RecyclerView.Adapter<ComAdapter.ComViewHolder> {
private List<String> mList;
public ComAdapter(List<String> mList) {
this.mList = mList;
}
@Override
public ComViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootViwe = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_items, parent, false);
return new ComViewHolder(rootViwe);
}
@Override
public void onBindViewHolder(ComViewHolder holder, int position) {
holder.mTv.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
static class ComViewHolder extends RecyclerView.ViewHolder {
private TextView mTv;
ComViewHolder(View itemView) {
super(itemView);
mTv = (TextView) itemView.findViewById(R.id.all);
}
}
}
这样写是不是很烦每个页面都要写一遍重复的数据,设置adapter holder 所以必须要优化;
- 封装后的ViewHoldler
public class BaseViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> mSparseArray;
private View mConVertView;
private BaseAdapter adapter;
public BaseViewHolder(View itemView) {
super(itemView);
mSparseArray = new SparseArray<>();
mConVertView = itemView;
}
/**
* 通过viewId获取控件
*
* @param viewId
* @return
*/
public <T extends View> T getView(int viewId) {
View view = mSparseArray.get(viewId);
if (view == null) {
view = mConVertView.findViewById(viewId);
mSparseArray.put(viewId, view);
}
return (T) view;
}
public BaseViewHolder setText(int viewId, String text) {
TextView tv = getView(viewId);
tv.setText(text);
return this;
}
public BaseViewHolder setImageResource(int viewId, int resId) {
ImageView view = getView(viewId);
view.setImageResource(resId);
return this;
}
/**
* 关于事件的
*/
public BaseViewHolder setOnClickListener(int viewId) {
View view = getView(viewId);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.getmOnItemChildClickListener().onItemChildClick(adapter, v, getAdapterPosition());
}
});
return this;
}
public BaseViewHolder setOnTouchListener(int viewId) {
View view = getView(viewId);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
return this;
}
public BaseViewHolder setOnLongClickListener(int viewId) {
View view = getView(viewId);
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
return this;
}
public static BaseViewHolder creatBaseViewHolder(View view) {
return new BaseViewHolder(view);
}
public <T> void setAdapter(BaseAdapter adapter) {
this.adapter = adapter;
}
}
adapter 封装:
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
private int layoutId;
private List<T> mData;
//header footer
private LinearLayout mHeaderLayout;
private FrameLayout mEmptyLayout;
private boolean mIsUseEmpty = true;
private final int EMPTY_VIEW = 0x011;
private final int NOMARL_VIEW = 0x012;
public BaseAdapter(@Nullable int layoutId, List<T> mData) {
this.layoutId = layoutId;
this.mData = mData;
}
public BaseAdapter(@Nullable int layoutId) {
this.layoutId = layoutId;
}
public BaseAdapter(List<T> mData) {
this.mData = mData;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View mView = LayoutInflater.from(context).inflate(layoutId, parent, false);
BaseViewHolder baseViewHolder = BaseViewHolder.creatBaseViewHolder(mView);
//设置监听:
bindViewClickListener(mView, baseViewHolder);
baseViewHolder .setAdapter(this);
return baseViewHolder;
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
convert(holder, mData.get(position), position);
}
protected abstract void convert(BaseViewHolder holder, T t, int position);
//绑定事件:
private void bindViewClickListener(View mView, final BaseViewHolder baseViewHolder) {
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, baseViewHolder.getAdapterPosition());
}
}
});
mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (OnItemLogClickListener != null) {
OnItemLogClickListener.onItemLongClick(v, baseViewHolder.getAdapterPosition());
}
return false;
}
});
}
@Override
public int getItemCount() {
if (mData == null) {
throw new NullPointerException("mData is not Initialization");
}
return mData.size();
}
private OnItemClickListener mOnItemClickListener;
private OnItemLogClickListener OnItemLogClickListener;
public void setOnItemLogClickListener(BaseAdapter.OnItemLogClickListener onItemLogClickListener) {
OnItemLogClickListener = onItemLogClickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public interface OnItemLogClickListener {
boolean onItemLongClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
private OnItemChildClickListener mOnItemChildClickListener;
public void setmOnItemChildClickListener(OnItemChildClickListener mOnItemChildClickListener) {
this.mOnItemChildClickListener = mOnItemChildClickListener;
}
public OnItemChildClickListener getmOnItemChildClickListener() {
return mOnItemChildClickListener;
}
public interface OnItemChildClickListener<T> {
void onItemChildClick(BaseAdapter<T> adapter, View view, int position);
}
}
采用谷歌推荐的SparseArray来代替Map key是int 类型同时里面也封装了item的子控件的点击,长按 事件 ,adapter 里面直接使用BaseViewHolde 最后的写adapter 就是这样的
public class ComAdapter extends BaseAdapter<String> {
protected ComAdapter(@Nullable int layoutId, List<String> mData) {
super(layoutId, mData);
}
public ComAdapter(@Nullable int layoutId) {
super(layoutId);
}
public ComAdapter(List<String> mData) {
super(R.layout.manactivity_items_layout, mData);
}
@Override
protected void convert(BaseViewHolder holder, String s, int position) {
holder.setText(R.id.m_tv_left, s).setOnClickListener(R.id.m_tv_left);
}
}
BaseViewHolder封装了一些常用的方法 setText ,setCheck 等等.....return this 直接就可以链式调用,
//设置点击事件:就是这么简单:
ComAdapter comAdapter = new ComAdapter(C.getList());
mRecyView.setAdapter(comAdapter);
comAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(OnitemsOnclickActivity.this, "我是items我被点击了" + position, Toast.LENGTH_SHORT).show();
}
});
comAdapter.setmOnItemChildClickListener(new BaseAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseAdapter adapter, View view, int position) {
Toast.makeText(OnitemsOnclickActivity.this, "我是items子控件的我被点击了" + position, Toast.LENGTH_SHORT).show();
}
});
comAdapter.setOnItemLogClickListener(new BaseAdapter.OnItemLogClickListener() {
@Override
public boolean onItemLongClick(View view, int position) {
Toast.makeText(OnitemsOnclickActivity.this, "我是items的我被长按了" + position, Toast.LENGTH_SHORT).show();
return false;
}
});
注意了 许多人喜欢在onBindViewHolder 里面初始化点击事件,这样其实是真的很耗性能的,每次设置数据的数据的时候都要把监听设置一遍,放在这里面主要是方便拿position ,而如果放在onCreateViewHolder 他也就会只初始化一次,从性能上来说是不是优化点,从代码上是不是更加优雅点以后设置点击事件的时候,只需要几行代码就可以搞定了,
@Override
protected void convert(BaseViewHolder holder, String s, int position) {
holder.setText(R.id.m_tv_left, s).setOnClickListener(R.id.m_tv_left);
}
直接向后. 更加方便,开发速度肯定会更加高效:
总结:
写了这么多,感觉是肾都要掏空了,咬咬牙写完它,代码是人敲出来的 ,如何更加高效的敲出好代码,就要思想稍微放的空一些,敲的是思想,也许不是最好但是是自我封装,
上述:只是简单针对一些常用的封装些,后续的还有添加头和上拉加载 像最上边那张图一样 我一直会把他写完 ,最后写的不好希望包容........最后向大家推荐一下这RecyclerViewgithub https://github.com/CymChad/BaseRecyclerViewAdapterHelper