每次都从头到位写adapter烦死。自己没事写了一个不完善的万能Adapter后期会继续完善。转载请写明出处
public abstract class OmRecyclerViewAdapter<T> extends RecyclerView.Adapter<OmRecyclerViewAdapter.ViewHolder> { private Context context; private List<T> data; private int layoutRes; //三个参数的含义,上下文对象、数据源、布局文件R.layout.XXX public OmRecyclerViewAdapter(Context context, List<T> data, int layoutRes) { if (data != null) { this.data = data; } else { this.data = new ArrayList<>(); } this.context = context; this.layoutRes = layoutRes; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(context).inflate(layoutRes, null, false); return new ViewHolder(inflate); } @Override public void onBindViewHolder(ViewHolder holder, int position) { bindData(holder, position, data); } @Override public int getItemCount() { return data != null ? data.size() : 0; } public static class ViewHolder extends RecyclerView.ViewHolder { Map<Integer, View> cacheView; public ViewHolder(View itemView) { super(itemView); cacheView = new HashMap<>(); } /** * 获取itemView中的childView * * @param resId * @return */ public View getView(int resId) { View view = null; // 判断Map缓存中是否包含我们要实例化的View if (cacheView.containsKey(resId)) { // 可以直接返回 view = cacheView.get(resId); } else { // 实例化一个,添加到缓存中 view = itemView.findViewById(resId); cacheView.put(resId, view); } return view; } } public abstract void bindData(ViewHolder holder, int position, List<T> data); } 导入方法: Add it in your root build.gradle at the end of repositories: allprojects { repositories { ... maven { url 'https://jitpack.io' } } Add the dependency: dependencies { compile 'com.github.WhatFxxk:OmRecyclerViewAdapter:v1.0.0' } }