BaseRecyclerViewAdapterHelper

介绍:

基于base-adapter-helper开源库的原理,提供了更多的功能。可以让RecyclerView adapter的使用更简单。

运行效果:

使用说明:

下载 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.5'
    }

如何使用它来创建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));
    }
}

如何添加item点击、长按事件

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();
            }
        });

如何使用它添加动画?

animation.gif

// 一行代码搞定(默认为渐显效果)
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)
                                };
                            }
                        });

使用它添加头部添加尾部

header_footer.gif

mQuickAdapter.addHeaderView(getView());
mQuickAdapter.addFooterView(getView());

使用它加载更多

load_more.gif

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);

使用分组

section_headers.gif

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();
            }
        });
    }

如何添加多种类型item?

multiple_item.gif

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;
        }
    }
}

使用setEmptyView

empty_view.gif

mQuickAdapter.setEmptyView(getView());

使用拖拽与滑动删除

drag_item.gif

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) {}
};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);
已下载
0