开源库BaseRecyclerViewAdapterHelper

相信大家RecyclerView应该不会陌生,大多数开发者应该都使用上它了,它也是google推荐替换ListView的控件,但是用过它的同学应该都知道它在某些方面并没有ListView使用起来方便,需要我们额外的编写代码,今天就给大家介绍一个开源库BaseRecyclerViewAdapterHelper,有了它让你使用RecyclerView的时候,和ListView一样的好用!

那么你要问了,BaseRecyclerViewAdapterHelper能做什么?

  • 优化Adapter代码(减少百分之70%代码)
  • 添加点击item点击、长按事件、以及item子控件的点击事件
  • 添加加载动画(一行代码轻松切换5种默认动画)
  • 添加头部、尾部、下拉刷新、上拉加载(感觉又回到ListView时代)
  • 设置自定义的加载更多布局
  • 添加分组(随心定义分组头部)
  • 自定义不同的item类型(简单配置、无需重写额外方法)
  • 设置空布局(比Listview的setEmptyView还要好用!)
  • 添加拖拽item
既然这么多好处,怎么用呢?

开源库BaseRecyclerViewAdapterHelper

</pre><pre name="code" class="html"><span style="font-size:10px;"><span style="color:#555555;">buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
       </span><span style="color:#ff0000;"> classpath 'com.novoda:bintray-release:0.3.4'</span><span style="color:#555555;">
    }
}

</span><span style="color:#ff0000;">allprojects {
    repositories {
        jcenter{
            url "http://jcenter.bintray.com/"
            maven { url "https://jitpack.io" }
        }
    }
    tasks.withType(Javadoc) {
        options {
            encoding "UTF-8" charSet 'UTF-8' links "http://docs.oracle.com/javase/7/docs/api"
        }
    }
}</span></span>
<span style="color:#ff0000;"> compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.9.7'</span>
这样就引入了BaseRecyclerViewAdapterHelper的支持

1,创建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));
    }
}
2,添加点击,长按点击事件

mQuickAdapter.setOnRecyclerViewItemClickListener();
mQuickAdapter.setOnRecyclerViewItemLongClickListener();
3,为item的多个控件添加点击 
protected void convert(BaseViewHolder helper, Status item) {
    helper.setOnClickListener(R.id.tweetAvatar, new OnItemChildClickListener())
      .setOnClickListener(R.id.tweetName, new OnItemChildClickListener());
      }
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();
            }
        });
4,添加动画效果(现在系统默认5种常用的动画,你还可以自定义动画)

上一张图

开源库BaseRecyclerViewAdapterHelper

默认动画效果,没错就只需要一行代码。

// 一行代码搞定(默认为渐显效果)
quickAdapter.openLoadAnimation();
什么,这种动画你不喜欢,换一种,系统默认的5种如下:

   public static final int ALPHAIN = 1;
    public static final int SCALEIN = 2;
    public static final int SLIDEIN_BOTTOM = 3;
    public static final int SLIDEIN_LEFT = 4;
    public static final int SLIDEIN_RIGHT = 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)
                                };
                            }
                        });
5,自定义添加头部和底部View
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();
                        }
                    }
                });
            }
        });<pre name="code" class="html">mQuickAdapter.setLoadingView(customView);


6,自定义多种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;
        }
    }
}

7,实现拖拽

我记得以前用listview或者gridview进行拖拽是很麻烦的,现在好了,使用RecyclerView可以很方便的实现拖拽,想怎么拖就怎么拖

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);
BaseRecyclerViewAdapterHelper还能实现很多功能,这里我就不详解了,有需要的可以直接到git上下载下来体验体验

点击打开链接

最后附上demo链接:https://github.com/xiangzhihong/BaseRecyclerViewAdapterHelper









上一篇:大数据和物联网:在制造业、医疗保健和智慧城市的成功应用


下一篇:LinkedIn开源Dr.elephant,Hadoop爽了