生活本来就不容易啊,正是我们的不努力才会让生活变得更加无赖。
整个月没有写过博客,这几天趁着看项目代码有使用到ListView添加headView决定自己更新一下,使用RecyclerView来代替,记得之前就写过一篇关于ReCyclerView的基本使用(一),这次写个续集吧。但是我们在setHeadView()
的时候并没有发现有这个方法,至于为什么呢我也不知道,但是各路大神已经优雅的为其添加HeadView和FootView了例如鸿洋大神,还有Loader的,还有很多很多,所以基本上不用写了,这篇笔记到此结束。嗯!
但是这笔记的意义不在此,只是想记录一下自己的参考过程,不,是思考过程。首先看看整个demo的预览图
基本思路
利用RecyclerView.Adapter
中的getItemType()
方法判断一下这是否为headView,footView
,在onCreateViewHolder(ViewGroup parent, int viewType)
方法中判断一下要给Viewholder的itemView
是什么类型。就这样。下面直接看代码吧
基本实现
BaseRecyclerAdapter
/**
* Created by AmatorLee on 2016/10/29.
* 基类Adaper
*/
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewHolder>{
private static final int ITEM_HEAD = 1000;
private static final int ITEM_FOOT = 2000;
private static final int ITEM_NOMAL = 3000;
private View mHeadView;
private View mFootView;
private List<T> mDatas;
private LayoutInflater mInflater;
/**
* 点击回调
*/
private OnItemClickListener mListener;
public void setListener(OnItemClickListener listener) {
mListener = listener;
}
/**
* 构造方法
* @param context
* @param mDatas
*/
public BaseRecyclerAdapter(Context context, List<T> mDatas) {
mInflater = LayoutInflater.from(context);
this.mDatas = mDatas;
}
/**
* headView
* @param headView
*/
public void addHeadView(View headView){
this.mHeadView = headView;
notifyItemInserted(0);
}
public View getHeadView(){
if (mHeadView != null){
return mHeadView;
}
return null;
}
/**
* FootView
* @param footView
*/
public void addFootView(View footView){
this.mFootView = footView;
notifyItemInserted(getItemCount() - 1);
}
public View getFootView(){
if (mFootView != null){
return mFootView;
}
return null;
}
@Override
public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mHeadView != null && viewType == ITEM_HEAD){
//如果为headView则itemView为headView
return new BaseRecyclerViewHolder(mHeadView);
}else if (mFootView != null && viewType == ITEM_FOOT){
//如果为footView则itemView为footView
return new BaseRecyclerViewHolder(mFootView);
}
//否则自己解析
return new BaseRecyclerViewHolder(mInflater.inflate(getLayoutID(),parent,false));
}
/*此方法为解析自己的view提供一个layoutId*/
public abstract int getLayoutID();
@Override
public void onBindViewHolder(BaseRecyclerViewHolder holder, final int position) {
/*判断是否有headView,如果0被占用则position-1*/
final int pos = getLayoutPos(holder);
if (getItemViewType(position) == ITEM_HEAD)return;/*交予自己处理headView*/
if (getItemViewType(position) == ITEM_FOOT)return;/*处理footView*/
/*点击监听*/
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null){
mListener.onItemClick(v,pos);
}
}
});
/*自己处理数据的方法*/
onBindData(holder,pos);
}
/*判断是否有headView,如果0被占用则position-1*/
private int getLayoutPos(BaseRecyclerViewHolder holder){
return mHeadView == null?holder.getLayoutPosition():holder.getLayoutPosition() - 1;
}
/*自己处理数据的方法*/
public abstract void onBindData(BaseRecyclerViewHolder holder, int position);
@Override
public int getItemCount() {
if (mHeadView == null && mFootView != null){
//如果footView则count+1
return mDatas.size() + 1;
}else if (mHeadView != null && mFootView == null){
//如果有headView则+1
return mDatas.size()+ 1;
}else if(mFootView != null && mHeadView != null){
//假如都有则+2
return mDatas.size() + 2;
}
/*否则不处理*/
return mDatas.size();
}
/***
* 此方法判断itemType的类型
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
if (mHeadView == null && mFootView == null)return ITEM_NOMAL;
if (position == 0 && mHeadView != null) return ITEM_HEAD;
if (position == getItemCount() -1) return ITEM_FOOT;
return ITEM_NOMAL;
}
/**
*回调监听
*/
public interface OnItemClickListener{
void onItemClick(View v, int pos);
}
}```
**BaseViewHolder**
/**
- Created by AmatorLee on 2016/10/29.
- 基类Viewholder
*/
public class BaseRecyclerViewHolder extends RecyclerView.ViewHolder{
private SparseArray<View> mViews;/*传说中装在view性能好的容器,key为int型,value为View*/
public View itemView;
/**
* 构造方法
* @param itemView
*/
public BaseRecyclerViewHolder(View itemView) {
super(itemView);
mViews = new SparseArray<>();
this.itemView = itemView;
}
/**
* 利用SpaseArray和泛型省略多次findViewbyid()
* @param viewId id
* @param <T> 类型
* @return
*/
public <T extends View>T getView(int viewId){
View view = mViews.get(viewId);
if (view == null){
view = itemView.findViewById(viewId);
mViews.put(viewId,view);
}
return (T) view;
}
/**
* 给外部调用的设置文本的方法
* @param viewId
* @param msg
*/
public void setText(int viewId,String msg){
TextView textView = getView(viewId);
textView.setText(msg);
}
/**
* 给外部调用的设置图片资源的方法
* @param viewID
* @param resID
*/
public void setImgRes(int viewID,int resID){
ImageView imageView = getView(viewID);
imageView.setImageResource(resID);
}
}```
- 这里定义了一个
BaseRecyclerAdapter And BaseViewHolder
用以处理是否添加headView,代码注释比较清楚。
基本使用
使用起来也是十分简单,我们新建一个自己的TestAdapter扩展自BaseRecyclerAdapter,需要实现其两个抽象方法:
@Override
public int getLayoutID() {
return R.layout.recycler_test;/*提供需要解析的layoutID*/
}
@Override
public void onBindData(BaseRecyclerViewHolder holder, int position) {
//处理数据
holder.setText(R.id.recycler_textView,mDatas.get(position));
}```
**TestAdapter**
/**
Created by AmatorLee on 2016/10/29.
-
目的Adapter,处理自己的相关逻辑
*/
public class TestAdapter extends BaseRecyclerAdapter<String> {private List<String> mDatas;
private LayoutInflater mInflater;public TestAdapter(Context context, List<String> mDatas) {
super(context, mDatas);
mInflater = LayoutInflater.from(context);
this.mDatas = mDatas;
}@Override
public int getLayoutID() {
return R.layout.recycler_test;
}@Override
public void onBindData(BaseRecyclerViewHolder holder, int position) {
holder.setText(R.id.recycler_textView,mDatas.get(position));
}
}```
Activity
public class HFActivity extends AppCompatActivity {
private List<String> mDatas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hf);
for (int i = 0; i < 30; i++) {
mDatas.add("这是第" + i + "item");
}
RecyclerView recycler_hf = (RecyclerView) findViewById(R.id.recycler_hf);
recycler_hf.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
TestAdapter adapter = new TestAdapter(this, mDatas);
recycler_hf.addItemDecoration(new ItemDecoration(this,LinearLayoutManager.VERTICAL));
View view = getLayoutInflater().inflate(R.layout.layout_headview, null);
adapter.addFootView(view);
adapter.addHeadView(view);
recycler_hf.setAdapter(adapter);
adapter.setListener(new BaseRecyclerAdapter.OnItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
Toast.makeText(HFActivity.this,pos + "onClick",Toast.LENGTH_SHORT).show();
}
});
}
}```
**效果演示**
![demo.gif](http://upload-images.jianshu.io/upload_images/2605454-9532a34e5740fc37.gif?imageMogr2/auto-orient/strip)
最近觉得要多使用github了,所以把自己写的一些demo放在github上面吧
[github地址](https://github.com/AmatorLee/RDemo/tree/master)
---
**总结**
RecyclerView已经在逐步取代listView了,但是相对来说listview使用起来更加方便,虽然目前很多大神已经将RecyclerView封装得完美,但是我不是啊,虽然可以用,但是我更加希望在使用之前可以先学习一遍。
#####如果你觉得本文有所错漏,麻烦留言指出,交流学习共同进步