RecyclerView作为Android最常用的控件,受益群体几乎是所有Android开发者,希望更多开发者能够一起来维护这个项目,把这个项目做得更好,帮助更多人。**Star我的项目可加Q群558178792,申请的时候把GitHub的账号名字备注上否则不予通过,谢谢配合。**中国有句古话叫“授人以鱼不如授人以渔”,不仅仅提供使用,还写了如何实现的原理:
- 「RecyclerView.Adapter优化了吗?」
- 「BaseRecyclerAdapter之添加动画」
- 「BaseRecyclerAdapter之添加不同布局(头部尾部)」
- 「BaseRecyclerAdapter之添加不同布局(优化篇)」
一个强大并且灵活的RecyclerViewAdapter,欢迎使用。(喜欢的可以Star一下)
#它能做什么?(下载 apk)
- 优化Adapter代码(减少百分之70%代码)
- 添加点击item点击、长按事件、以及item子控件的点击事件
- 添加加载动画(一行代码轻松切换5种默认动画)
- 添加头部、尾部、下拉刷新、上拉加载(感觉又回到ListView时代)
- 设置自定义的加载更多布局
- 添加分组(随心定义分组头部)
- 自定义不同的item类型(简单配置、无需重写额外方法)
- 设置空布局(比Listview的setEmptyView还要好用!)
- 添加拖拽item
#如何使用它? 先在 build.gradle 的 repositories 添加:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
然后在dependencies添加:
dependencies {
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.8.2'
}
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));
}
}
mQuickAdapter.setOnRecyclerViewItemClickListener();
mQuickAdapter.setOnRecyclerViewItemLongClickListener();
#新增添加子布局多个控件的点击事件 Adapter
protected void convert(BaseViewHolder helper, Status item) {
helper.setOnClickListener(R.id.tweetAvatar, new OnItemChildClickListener())
.setOnClickListener(R.id.tweetName, new OnItemChildClickListener());
}
Activity
mQuickAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
String content = null;
Status status = (Status) adapter.getItem(position);
switch (view.getId()) {
case R.id.tweetAvatar:
content = "img:" + status.getUserAvatar();
break;
case R.id.tweetName:
content = "name:" + status.getUserName();
break;
}
Toast.makeText(AnimationUseActivity.this, content, Toast.LENGTH_LONG).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)
};
}
});
mQuickAdapter.addHeaderView(getView());
mQuickAdapter.addFooterView(getView());
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);
else
helper.setOnClickListener(R.id.more, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,item.header+"more..",Toast.LENGTH_LONG).show();
}
});
}
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;
}
}
}
mQuickAdapter.setEmptyView(getView());
OnItemDragListener listener = new OnItemDragListener() {
@Override
public void onItemDragStart(RecyclerView.ViewHolder viewHolder){}
@Override
public void onItemDragMoving(RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {}
@Override
public void onItemDragEnd(RecyclerView.ViewHolder viewHolder) {}
}
ItemDraggableCallback itemDraggableCallback = new ItemDraggableCallback(mAdapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDraggableCallback);
mAdapter.enableDragItem(mItemTouchHelper, R.id.textView, true);
mAdapter.setOnItemDragListener(listener);
持续更新!,所以推荐Star项目