之前对ListView的BaseAdapter进行过封装,只需重写一个getView方法;
现在慢慢的RecyclerView成为主流,下面是RecyclerView.Adapter的封装;
BaseRecyclerViewHolder类:
public class BaseRecyclerHolder extends RecyclerView.ViewHolder { private SparseArrayCompat<View> mViews; public BaseRecyclerHolder(View itemView) { super(itemView); mViews = new SparseArrayCompat<>(); } public <V extends View> V getView(@IdRes int res){ View v = mViews.get(res); if (v == null){ v = itemView.findViewById(res); mViews.put(res,v); } return (V)v; } }
BaseRecyclerViewAdapter类:
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerHolder> { public Context context; public int layoutRes; public List<T> items; public BaseRecyclerAdapter(Context context,@LayoutRes int layoutRes){ this.context = context; this.layoutRes = layoutRes; items = new ArrayList<T>(); } public BaseRecyclerAdapter(Context context,@LayoutRes int layoutResId, @Nullable List<T> data) { this.context = context; this.items = data == null ? new ArrayList<T>() : data; if (layoutResId != 0) { this.layoutRes = layoutResId; } } @Override public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new BaseRecyclerHolder(LayoutInflater.from(context).inflate(layoutRes,null)); } @Override public void onBindViewHolder(BaseRecyclerHolder holder, int position) { convert(holder,position); } @Override public int getItemCount() { return items.size(); } public void setData(List<T> items) { this.items = items == null ? new ArrayList<T>() : items; notifyDataSetChanged(); } public void addData(@IntRange(from = 0) int position, @NonNull T data) { items.add(position, data); notifyItemInserted(position); compatibilityDataSizeChanged(1); } public void addData( @NonNull T data) { items.add(data); notifyItemInserted(items.size()); } public void remove(int position) { items.remove(position); int internalPosition = position; notifyItemRemoved(internalPosition); notifyItemRangeChanged(internalPosition, items.size() - internalPosition); } private void compatibilityDataSizeChanged(int size) { final int dataSize = items == null ? 0 : items.size(); if (dataSize == size) { notifyDataSetChanged(); } } /** * 需要重写的方法 * @param holder * @param position */ public abstract void convert(BaseRecyclerHolder holder, int position); }
如何使用:MainActivity类:
public class MainActivity extends AppCompatActivity { private final String TAG = this.getClass().getSimpleName(); private List<String> d = Arrays.asList( "A","B","C","D","E","F","G" ,"H","I","J","K","L","M","N" ,"O","P","Q","R","S","T" ,"U","V","W","X","Y","Z"); private RecyclerView rv ; private BaseRecyclerAdapter ap; private List<String> datas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); // ap = new BaseRecyclerAdapter<String>(MainActivity.this, R.layout.item_rv){ // 还可以这样用 // @Override // public void convert(BaseRecyclerHolder holder, int position) { // // } // }; rv = findViewById(R.id.rv); rv.setLayoutManager(new GridLayoutManager(this,3)); ap = new Ap(this,R.layout.item_rv); rv.setAdapter(ap); ap.setData(datas); } private void initData() { datas = new ArrayList<>(); for (int i = 0; i < d.size(); i++) { datas.add(d.get(i)); } } class Ap extends BaseRecyclerAdapter<String>{ public Ap(Context c, int layoutRes) { super(c, layoutRes); } @Override public void convert(BaseRecyclerHolder holder, final int position) { //链式调用 holder.setText(R.id.tv,items.get(position)) .setOnClickListener(R.id.tv, new View.OnClickListener() { @Override public void onClick(View view) { ap.remove(position); } }); } } }
其实就只有这一个是你关注的,需要实现的Adapter:
class Ap extends BaseRecyclerAdapter<String>{ public Ap(Context c, int layoutRes) { super(c, layoutRes); } @Override public void convert(BaseRecyclerHolder holder, final int position) { //链式调用 holder.setText(R.id.tv,items.get(position)) .setOnClickListener(R.id.tv, new View.OnClickListener() { @Override public void onClick(View view) { ap.remove(position); } }); } }
是不是超简单:
这个是Demo,里面封装的更加全面;点击打开链接