添加Gradle依赖
compile ‘com.android.support:appcompat-v7:23.2.1‘compile ‘com.android.support:recyclerview-v7:23.2.1‘
备注:recyclerview的版本号要与appcompat的一致,此处为: v7:23.2.1
以泛型方式定义通用RecyclerViewHold对象,内部以SparseArray存储View对象,提高xml的解析速度。
/** * 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);}}
重写onCreateViewHolder与onBindViewHolder,其中onCreateViewHolder为每个Item生成View对象,并封装至ViewHolder中返回;onBindViewHolder负责渲染数据至View
/** * 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;} /*系统重写函数*/@Overridepublic 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);} @Overridepublic void onBindViewHolder(UnityRecyclerAdapterViewHolder holder, int position) { lg.e("onBindViewHolder with postion[" + position + "]:" + holder.toString());convert(IAdapterataModifyHelper.getDataResources().get(position), holder, position);} @Overridepublic 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);} @Overridepublic void notifyAdapter() { notifyDataSetChanged();}}
具体使用方式如下,
adapter = new SingleItemRecyclerAdapter<CorePageEntity>(R.layout.simple_item_horizontal) { @Overridepublic 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(//添加数据集);
源码链接:https://github.com/xiaoxuan948/AndroidUnityLab/tree/master/unity_base_dev_helper/src/main/java/com/coca/unity_base_dev_helper/adapter/recycler_view
扩展功能包括刷新、添加头尾布局,请查看http://m.blog.csdn.net/article/details?plg_nld=1&id=51153978&plg_auth=1&plg_uin=1&plg_usr=1&plg_vkey=1&plg_nld=1&plg_dev=1
# 后续添加onItemClicklistener事件
时间: 2024-10-24 23:25:10