优化Adapter代码(减少百分之70%代码)
添加点击item点击、长按事件、以及item子控件的点击事件
添加加载动画(一行代码轻松切换5种默认动画)
添加头部、尾部、下拉刷新、上拉加载(感觉又回到ListView时代)
设置自定义的加载更多布局
添加分组(随心定义分组头部)
自定义不同的item类型(简单配置、无需重写额外方法)
设置空布局(比Listview的setEmptyView还要好用!)
添加拖拽item
扩展库
如何使用它?
先在 项目build.gradle 的 repositories 添加:
allprojects { repositories { google() jcenter() maven { url "https://jitpack.io" } } }
然后在module build.grale的dependencies中添加:
dependencies { compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.47' }
androidX 迁移库版本
如何使用它来创建Adapter?
public class QuickAdapter extends BaseQuickAdapter<Status> { public QuickAdapter() { super(R.layout.tweet, DataServer.getSampleData()); } @Override protected void convert(BaseViewHolder helper, Status item) { helper.setText(R.id.tweetName, item.getUserName()) .setText(R.id.tweetText, item.getText()) .setText(R.id.tweetDate, item.getCreatedAt()) .setVisible(R.id.tweetRT, item.isRetweet()) .linkify(R.id.tweetText); Glide.with(mContext).load(item.getUserAvatar()).crossFade().into((ImageView) helper.getView(R.id.iv)); } }
Adapter
mRecyclerView.addOnItemTouchListener(new OnItemClickListener() { @Override public void SimpleOnItemClick (BaseQuickAdapter adapter, View view,int position){ Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } });
设置 item click 新增添加子布局多个控件的点击事件
设置 it item child click
首先需要添加需要点击触发的 childview id
@Override protected void convert(BaseViewHolder helper, Status item) { helper.setText(R.id.tweetName, item.getUserName()) .setText(R.id.tweetText, item.getText()) .setText(R.id.tweetDate, item.getCreatedAt()) .setVisible(R.id.tweetRT, item.isRetweet()) .addOnClickListener(R.id.tweetAvatar) .addOnClickListener(R.id.tweetName) .addOnLongClickListener(R.id.tweetText) .linkify(R.id.tweetText); Glide.with(mContext).load(item.getUserAvatar()).crossFade().placeholder(R.mipmap.def_head).transform(new GlideCircleTransform(mContext)).into((ImageView) helper.getView(R.id.tweetAvatar)); }
Activity
mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener( ) { @Override public void SimpleOnItemChildClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } });
设置 it item long click
mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener( ) { @Override public void SimpleOnItemLongClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } });
设置 it item child long click
首先需要添加需要点击触发的 childview id
@Override protected void convert(BaseViewHolder helper, Status item) { helper.setText(R.id.tweetName, item.getUserName()) .setText(R.id.tweetText, item.getText()) .setText(R.id.tweetDate, item.getCreatedAt()) .setVisible(R.id.tweetRT, item.isRetweet()) .addOnClickListener(R.id.tweetAvatar) .addOnClickListener(R.id.tweetName) .addOnLongClickListener(R.id.tweetText) .linkify(R.id.tweetText); Glide.with(mContext).load(item.getUserAvatar()).crossFade().placeholder(R.mipmap.def_head).transform(new GlideCircleTransform(mContext)).into((ImageView) helper.getView(R.id.tweetAvatar)); }
然后
mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener( ) { @Override public void SimpleOnItemChildLongClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } });
如果你想实现多种点击事件,你可以实现 SimpleClickListener类。提供了丰富的事件点击封装
mRecyclerView.addOnItemTouchListener(new SimpleClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } @Override public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } @Override public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) { Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } });
如何使用它添加动画?
// 一行代码搞定(默认为渐显效果)quickAdapter.openLoadAnimation();
不喜欢渐显动画可以这样更换
// 默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左) quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
还是没你喜欢的,你可以自定义
// 自定义动画如此轻松 quickAdapter.openLoadAnimation(new BaseAnimation() { @Override public Animator[] getAnimators(View view) { return new Animator[]{ ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1), ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1) }; } });
使用它添加头部添加尾部
// addmQuickAdapter.addHeaderView(getView()); mQuickAdapter.addFooterView(getView()); // remove removeHeaderView(getView); removeFooterView(getView); // or removeAllHeaderView(); removeAllFooterView();
使用它加载更多
mQuickAdapter.openLoadMore(PAGE_SIZE, true); mQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { @Override public void onLoadMoreRequested() { mRecyclerView.post(new Runnable() { @Override public void run() { if (mCurrentCounter >= TOTAL_COUNTER) { mQuickAdapter.notifyDataChangedAfterLoadMore(false); } else { mQuickAdapter.notifyDataChangedAfterLoadMore(DataServer.getSampleData(PAGE_SIZE), true); mCurrentCounter = mQuickAdapter.getItemCount(); } } }); } });
设置自定义加载更多布局
mQuickAdapter.setLoadingView(customView);
使用分组
public class SectionAdapter extends BaseSectionQuickAdapter<MySection> { public SectionAdapter(int layoutResId, int sectionHeadResId, List data) { super(layoutResId, sectionHeadResId, data); } @Override protected void convert(BaseViewHolder helper, MySection item) { helper.setImageUrl(R.id.iv, (String) item.t); } @Override protected void convertHead(BaseViewHolder helper,final MySection item) { helper.setText(R.id.header, item.header); helper.setOnClickListener(R.id.more, new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context,item.header+"more..",Toast.LENGTH_LONG).show(); } }); }
如何添加多种类型item?
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem> { public MultipleItemQuickAdapter(List data) { super(data); addItemType(MultipleItem.TEXT, R.layout.text_view); addItemType(MultipleItem.IMG, R.layout.image_view); } @Override protected void convert(BaseViewHolder helper, MultipleItem item) { switch (helper.getItemViewType()) { case MultipleItem.TEXT: helper.setImageUrl(R.id.tv, item.getContent()); break; case MultipleItem.IMG: helper.setImageUrl(R.id.iv, item.getContent()); break; default: break; } } }
使用setEmptyView
mQuickAdapter.setEmptyView(getView());
使用拖拽与滑动删除
OnItemDragListener onItemDragListener = new OnItemDragListener() { @Override public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos){} @Override public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {} @Override public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {} } OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() { @Override public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {} @Override public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {} @Override public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {} }; public class ItemDragAdapter extends BaseItemDraggableAdapter<String> { public ItemDragAdapter(List data) { super(R.layout.item_draggable_view, data); } @Override protected void convert(BaseViewHolder helper, String item) { helper.setText(R.id.tv, item); } } mAdapter = new ItemDragAdapter(mData); ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter); ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback); itemTouchHelper.attachToRecyclerView(mRecyclerView); // 开启拖拽 mAdapter.enableDragItem(itemTouchHelper, R.id.textView, true); mAdapter.setOnItemDragListener(onItemDragListener); // 开启滑动删除 mAdapter.enableSwipeItem(); mAdapter.setOnItemSwipeListener(onItemSwipeListener);
Expandable Item
// 如果不想使用继承,可以只实现IExpandable接口 // AbstractExpandableItem只是个帮助类 public class Level0Item extends AbstractExpandableItem<Level1Item> {...} public class Level1Item extends AbstractExpandableItem<Person> {...} public class Person {...}
in adapter code
ublic class ExpandableItemAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity> { public ExpandableItemAdapter(List<MultiItemEntity> data) { super(data); addItemType(TYPE_LEVEL_0, R.layout.item_expandable_lv0); addItemType(TYPE_LEVEL_1, R.layout.item_expandable_lv1); addItemType(TYPE_PERSON, R.layout.item_text_view); } @Override protected void convert(final BaseViewHolder holder, final MultiItemEntity item) { switch (holder.getItemViewType()) { case TYPE_LEVEL_0: //set view content holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos = holder.getAdapterPosition(); if (lv0.isExpanded()) { collapse(pos); } else { expand(pos); } }}); break; case TYPE_LEVEL_1: // similar with level 0 break; case TYPE_PERSON: //just set the content break; default: break; } }
In activity code
public class ExpandableUseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { ... ArrayList<MultiItemEntity> list = generateData(); ExpandableItemAdapter adapter = new ExpandableItemAdapter(list); mRecyclerView.setAdapter(adapter); } private ArrayList<MultiItemEntity> generateData() { ArrayList<MultiItemEntity> res = new ArrayList<>(); for (int i = 0; i < lv0Count; i++) { Level0Item lv0 = new Level0Item(...); for (int j = 0; j < lv1Count; j++) { Level1Item lv1 = new Level1Item(...); for (int k = 0; k < personCount; k++) { lv1.addSubItem(new Person()); } lv0.addSubItem(lv1); } res.add(lv0); } return res; } }
Use Custom BaseViewHolder
// 当使用自定义的BaseViewHolder时,需要重写此函数以创建ViewHolder protected K createBaseViewHolder(View view) { return (K) new BaseViewHolder(view); }
DiffUtil
先继承BaseQuickDiffCallback<T>
并实现:
public class DiffDemoCallback extends BaseQuickDiffCallback<DiffUtilDemoEntity> { public DiffDemoCallback(@Nullable List<DiffUtilDemoEntity> newList) { super(newList); } /** * 判断是否是同一个item * * @param oldItem New data * @param newItem old Data * @return */ @Override protected boolean areItemsTheSame(DiffUtilDemoEntity oldItem, DiffUtilDemoEntity newItem) { …… } /** * 当是同一个item时,再判断内容是否发生改变 * * @param oldItem New data * @param newItem old Data * @return */ @Override protected boolean areContentsTheSame(DiffUtilDemoEntity oldItem, DiffUtilDemoEntity newItem) { …… } /** * 可选实现 * 如果需要精确修改某一个view中的内容,请实现此方法。 * 如果不实现此方法,或者返回null,将会直接刷新整个item。 * * @param oldItem Old data * @param newItem New data * @return Payload info. if return null, the entire item will be refreshed. */ @Override protected Object getChangePayload(DiffUtilDemoEntity oldItem, DiffUtilDemoEntity newItem) { …… } } BaseQuickAdapter中实现convertPayloads()方法,用于局部更新: /** * * 当有 payload info 时,只会执行此方法 * * @param helper A fully initialized helper. * @param item The item that needs to be displayed. * @param payloads payload info. */ @Override protected void convertPayloads(BaseViewHolder helper, DiffUtilDemoEntity item, @NonNull List<Object> payloads) { for (Object p : payloads) { …… } }
更新整个数据集时,使用如下方法:
DiffDemoCallback callback = new DiffDemoCallback(getNewList()); mAdapter.setNewDiffData(callback);
更新单个item时,使用如下方法:
mAdapter.notifyItemChanged(0, "payload info");
异步Diff & 原始DiffUtil.Callback
用户可以直接使用DiffUtil.Callback
,自己进行diff计算,将结果告知adapter即可。 所以adapter并不关心diff计算过程,用户可以同步或是异步进行。 使用如下方法:
setNewDiffData(DiffUtil.DiffResult, List)
例子:
new Thread(new Runnable() { @Override public void run() { final List<DiffUtilDemoEntity> newData = getNewList(); MyDiffCallback callback = new MyDiffCallback(newData, mAdapter.getData()); final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callback, false); runOnUiThread(new Runnable() { @Override public void run() { mAdapter.setNewDiffData(diffResult, newData); } }); } }).start();
警告:你应该自己进行多线程管理,防止内存泄漏