标签:
compile ‘com.android.support:appcompat-v7:23.2.1‘
compile ‘com.android.support:recyclerview-v7:23.2.1‘
/**
* Created by Administrator on 2016/6/14.
* 通用Recycler.ViewHolder
*/
public class UnityRecyclerAdapterViewHolder extends RecyclerView.ViewHolder {
private static final UtilsLog lg = UtilsLog.getLogger(UnityRecyclerAdapterViewHolder.class);
private SparseArray<View> mViews;
private View mConvertView;
public UnityRecyclerAdapterViewHolder(View itemView, ViewGroup parent) {
super(itemView);
this.mConvertView = itemView;
this.mViews = new SparseArray<>();
}
public View getConvertItemView() {
return mConvertView;
}
/**
* 通过viewId获取控件
*
* @param viewId
* @return
*/
public <T extends View> T generateView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public <T extends View> T generateView(int viewId, Class<T> clz) {
return (T) generateView(viewId);
}
}
/**
* Created by Administrator on 2016/6/15.
* Recycler.Adapter基类
* 1.getItemLayoutId与getItemLayoutView二者选择其一进行复写:此处为支持布局以及代码方式
* 2.该类还可以继续扩展,对仅有单个布局的情况,可以通过构造方法直接注入itemLayoutId
*/
public abstract class AbsUnityBaseRecyclerAdapter<T> extends RecyclerView.Adapter<UnityRecyclerAdapterViewHolder> implements INotifyAdapterDataSetChange {
private static final UtilsLog lg = UtilsLog.getLogger(AbsUnityBaseRecyclerAdapter.class);
private IAdapterDataModifyHelper<T> IAdapterataModifyHelper;//操作数据的Helper类
private LayoutInflater layoutInflater;
public AbsUnityBaseRecyclerAdapter() {
this.IAdapterataModifyHelper = new DataModifyHelper<>();
}
public IAdapterDataModifyHelper<T> getDataModifyHelper() {
this.IAdapterataModifyHelper.setNotifyAdapterDataSetChange(this);
return IAdapterataModifyHelper;
}
/*系统重写函数*/
@Override
public UnityRecyclerAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
lg.e("onCreateViewHolder");
if (this.layoutInflater == null) {
this.layoutInflater = LayoutInflater.from(parent.getContext());
}
return new UnityRecyclerAdapterViewHolder(getItemLayoutView(viewType), parent);
}
@Override
public void onBindViewHolder(UnityRecyclerAdapterViewHolder holder, int position) {
lg.e("onBindViewHolder with postion[" + position + "]:" + holder.toString());
convert(IAdapterataModifyHelper.getDataResources().get(position), holder, position);
}
@Override
public int getItemCount() {
return IAdapterataModifyHelper.getDataResources().size();
}
/**
* 填充数据
*
* @param viewHolder
* @param position
*/
public abstract void convert(T itemEntity, UnityRecyclerAdapterViewHolder viewHolder, int position);
/**
* 通过ItemViewType返回布局id
*
* @param viewType
* @return
*/
public int getItemLayoutId(int viewType) {
return 0;
}
/**
* 通过viewType返回指定Item布局
*
* @param viewType
* @return
*/
public View getItemLayoutView(int viewType) {
return layoutInflater.inflate(getItemLayoutId(viewType), null);
}
@Override
public void notifyAdapter() {
notifyDataSetChanged();
}
}
adapter = new SingleItemRecyclerAdapter<CorePageEntity>(R.layout.simple_item_horizontal) {
@Override
public void convert(CorePageEntity itemEntity, UnityRecyclerAdapterViewHolder viewHolder, int position) {
if (position != 0) {
lg.e("convert with position " + position + ":" + viewHolder.toString());
}
viewHolder.generateView(R.id.imgItemCommon, ImageView.class).setVisibility(View.GONE);
viewHolder.generateView(R.id.tvItemCommon, TextView.class).setText(itemEntity.getCorePageTitle());
}
};
gridActMain.setAdapter(adapter);
gridActMain.setLayoutManager(new GridLayoutManager(getCurActivity(), 1));
adapter.getDataModifyHelper().setDataResource(//添加数据集);
Android 5.0 v7扩展包之RecyclerView的使用以及通用适配器的封装实现
标签:
原文地址:http://www.cnblogs.com/linux007/p/5791676.html