摘要:
因为网上关于vlayout使用的文章非常多,但好多又都大同小异.我在按照别人文章和demo在使用vlayout的过程中遇到了一些坑,于是记录下来希望能够对大家提供一些帮助.
1.关于RecycledViewPool
final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
recyclerView.setRecycledViewPool(viewPool);
//设置缓存view个数(当视图中view的个数很多时,设置合理的缓存大小,防止来回滚动时重新创建 View)
viewPool.setMaxRecycledViews(1,1);
viewPool.setMaxRecycledViews(2,10);
viewPool.setMaxRecycledViews(3,10);
viewPool.setMaxRecycledViews(4,1);
我看了好多demo都只为type == 0的时候设置了,其实每个type都设置好一些.
2.绑定数据
之前看的demo都是先造假数据,然后在创建adapter时直接将数据传进去.我刚开始写项目时,创建adapter时没有传递数据,然后在数据请求下来后再绑定数据并刷新adapter.但是会有个问题.比如如果倒数第二个adapter如果没有设置数据,最后一个adapter会不显示,.甚至有个adapter设置了数据它也不显示.很奇怪.后来终于找到了一个后设置数据的demo案例,他是创建adapter时先new一个list传进去,然后在数据请求下来后在将数据赋给之前的list并刷新adapter,上面的问题就解决了.
gridAdapter = new VBaseAdapter<CommonBean>(this,2).setData(new ArrayList<CommonBean>())
.setLayout(R.layout.item_grid)
.setLayoutHelper(getGridLayoutHelper())
.setHolder(GridHolder.class)
.setOnItemClickListener(new OnItemClickListener<CommonBean>() {
@Override public void onItemClick(View view, int position, CommonBean mData) {
Toast.makeText(MainActivity.this, mData.name, Toast.LENGTH_SHORT).show();
}
});
@Override public void getGridDataSuccess(List<CommonBean> list) {
gridAdapter.setData(list);
}
3.关于count问题
因为有的adapter只有一个条目,但数据也可能是一个list,比如一个banner.所以我就加了一个setItem()方法.
/**
* 多个条目时使用
*/
public VBaseAdapter setData(List<T> mDatas) {
this.mDatas = mDatas;
notifyDataSetChanged();
return this;
}
/**
* 单个条目或新增条目时使用
*/
public VBaseAdapter setItem(T mItem) {
this.mDatas.add(mItem);
notifyDataSetChanged();
return this;
}
一个条目时,无论数据是不是list,都调用setItem(),这样mDatas的size就是1.
4.adapter和viewHolder的封装
不多说了,直接上代码吧.
先来adapter
package application.android.com.vlayoutdemo.base;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import application.android.com.vlayoutdemo.event.OnItemChildClickListener;
import application.android.com.vlayoutdemo.event.OnItemClickListener;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.List;
public class VBaseAdapter<T> extends DelegateAdapter.Adapter<VBaseHolder<T>> {
private Context mContext;
private int mResLayout;
private List<T> mDatas;
private LayoutHelper mLayoutHelper;
private Class<? extends VBaseHolder> mClazz;
private OnItemClickListener itemListener;
private OnItemChildClickListener itemChildListener;
private int viewType = -1;
public VBaseAdapter(Context context,int viewType) {
mContext = context;
this.viewType = viewType;
}
public VBaseAdapter(Context context,int viewType, List<T> mDatas, int mResLayout,
Class<? extends VBaseHolder> mClazz, LayoutHelper layoutHelper,
OnItemClickListener listener) {
if (mClazz == null) {
throw new RuntimeException("clazz is null,please check your params !");
}
if (mResLayout == 0) {
throw new RuntimeException("res is null,please check your params !");
}
this.viewType = viewType;
this.mContext = context;
this.mResLayout = mResLayout;
this.mLayoutHelper = layoutHelper;
this.mClazz = mClazz;
this.itemListener = listener;
this.mDatas = mDatas;
}
/**
* 多个条目时使用
*/
public VBaseAdapter setData(List<T> mDatas) {
this.mDatas = mDatas;
notifyDataSetChanged();
return this;
}
/**
* 单个条目或新增条目时使用
*/
public VBaseAdapter setItem(T mItem) {
this.mDatas.add(mItem);
notifyDataSetChanged();
return this;
}
public VBaseAdapter setLayout(int mResLayout) {
if (mResLayout == 0) {
throw new RuntimeException("res is null,please check your params !");
}
this.mResLayout = mResLayout;
return this;
}
public VBaseAdapter setLayoutHelper(LayoutHelper layoutHelper) {
this.mLayoutHelper = layoutHelper;
return this;
}
public VBaseAdapter setHolder(Class<? extends VBaseHolder> mClazz) {
if (mClazz == null) {
throw new RuntimeException("clazz is null,please check your params !");
}
this.mClazz = mClazz;
return this;
}
/**
* 条目点击
*/
public VBaseAdapter setOnItemClickListener(OnItemClickListener listener) {
this.itemListener = listener;
return this;
}
/**
* 条w点击
*/
public VBaseAdapter setOnItemChildClickListener(OnItemChildClickListener listener) {
this.itemChildListener = listener;
return this;
}
/**
* <br/> 方法名称: onCreateLayoutHelper
* <br/> 方法详述: 继承elegateAdapter.Adapter后重写方法,告知elegateAdapter.Adapter使用何种布局管理器
* <br/> 参数:
* <br/> 返回值: VBaseAdapter
*/
@Override public LayoutHelper onCreateLayoutHelper() {
return mLayoutHelper;
}
public HashMap<Integer, Object> tags = new HashMap<>();
/**
* <br/> 方法名称: setTag
* <br/> 方法详述: 设置mObject
* <br/> 参数: mObject
* <br/> 返回值: VBaseAdapter
*/
public VBaseAdapter setTag(int tag, Object mObject) {
if (mObject != null) {
tags.put(tag, mObject);
}
return this;
}
/**
* <br/> 方法名称: onCreateViewHolder
* <br/> 方法详述: 解析布局文件,返回传入holder的构造器
*/
@Override public VBaseHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(mResLayout, parent, false);
if (tags != null && tags.size() > 0) {
for (int tag : tags.keySet()) {
view.setTag(tag, tags.get(tag));
}
}
try {
Constructor<? extends VBaseHolder> mClazzConstructor = mClazz.getConstructor(View.class);
if (mClazzConstructor != null) {
return mClazzConstructor.newInstance(view);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* <br/> 方法名称: onBindViewHolder
* <br/> 方法详述: 绑定数据
* <br/> 参数:
* <br/> 返回值: VBaseAdapter
*/
@Override public void onBindViewHolder(VBaseHolder holder, int position) {
holder.setOnItemClickListener(itemListener);
holder.setOnItemChildClickListener(itemChildListener);
holder.setContext(mContext);
holder.setData(position, mDatas.get(position));
}
@Override public int getItemCount() {
return mDatas.size();
}
@Override public int getItemViewType(int position) {
return viewType;
}
}
再来viewHolder
package application.android.com.vlayoutdemo.base;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import application.android.com.vlayoutdemo.event.OnItemChildClickListener;
import application.android.com.vlayoutdemo.event.OnItemClickListener;
import com.bumptech.glide.Glide;
/**
* Created by Moushao on 2017/8/23.
*/
public class VBaseHolder<W> extends RecyclerView.ViewHolder {
public OnItemChildClickListener itemChildClickListener;
public Context mContext;
public View itemView;
public W mData;
public int position;
private SparseArray<View> viewMap = new SparseArray();
public VBaseHolder(View itemView) {
super(itemView);
this.itemView = itemView;
itemView.setTag(this);
init();
}
public void init() {
}
public void setContext(Context context) {
mContext = context;
}
public void setOnItemClickListener(final OnItemClickListener listener) {
if (listener != null){
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
listener.onItemClick(itemView,position,mData);
}
});
}
}
public void setOnItemChildClickListener(OnItemChildClickListener itemChildClickListener) {
this.itemChildClickListener = itemChildClickListener;
}
public void setData(int position, W mData) {
this.mData = mData;
this.position = position;
}
public void clear() {
this.viewMap.clear();
this.itemView = null;
}
public <T extends View> T get(@IdRes int id) {
View childView = this.viewMap.get(id);
if (childView == null && this.itemView != null) {
childView = this.itemView.findViewById(id);
this.viewMap.put(id, childView);
}
return (T) childView;
}
public VBaseHolder setText(@IdRes int id, CharSequence text) {
TextView tv = this.get(id);
if (tv != null) {
tv.setText(text);
}
return this;
}
public VBaseHolder setText(@IdRes int id, @StringRes int strId) {
TextView tv = this.get(id);
if (tv != null) {
tv.setText(strId);
}
return this;
}
public VBaseHolder setTextColor(@IdRes int id, int color) {
TextView tv = this.get(id);
if (tv != null) {
tv.setTextColor(color);
}
return this;
}
public VBaseHolder setImageResource(@IdRes int id, @DrawableRes int drawId) {
ImageView iv = this.get(id);
if (iv != null) {
iv.setImageResource(drawId);
}
return this;
}
public VBaseHolder setVisible(@IdRes int id, boolean visible) {
View tv = this.get(id);
if (tv != null) {
tv.setVisibility(visible ? View.VISIBLE : View.GONE);
}
return this;
}
public VBaseHolder setEnable(@IdRes int id, boolean enable) {
View tv = this.get(id);
if (tv != null) {
tv.setEnabled(enable);
}
return this;
}
public VBaseHolder setClickListener(View.OnClickListener listener) {
if (this.itemView != null) {
this.itemView.setOnClickListener(listener);
}
return this;
}
public VBaseHolder setClickListener(@IdRes int id, View.OnClickListener listener) {
View tv = this.get(id);
if (tv != null) {
tv.setOnClickListener(listener);
}
return this;
}
public VBaseHolder loadImage(@IdRes int id, String imageUrl) {
ImageView iv = this.get(id);
if (iv != null && imageUrl != null) {
Glide.with(mContext).load(imageUrl).into(iv);
}
return this;
}
public VBaseHolder loadImage(@IdRes int id, String imageUrl, @DrawableRes int df) {
ImageView iv = this.get(id);
if (iv != null && imageUrl != null) {
Glide.with(mContext).load(imageUrl).into(iv);
}
return this;
}
}
5.尾巴
demo源码地址.
https://github.com/Alphabet111/Vlayout-Demo
大家可以下下来看下.