标签:
相信大家RecyclerView应该不会陌生,大多数开发者应该都使用上它了,它也是google推荐替换ListView的控件,但是用过它的同学应该都知道它在某些方面并没有ListView使用起来方便,需要我们额外的编写代码,今天就给大家介绍一个开源库BaseRecyclerViewAdapterHelper,有了它让你使用RecyclerView的时候,和ListView一样的好用!
那么你要问了,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种常用的动画,你还可以自定义动画)
上一张图
默认动画效果,没错就只需要一行代码。
// 一行代码搞定(默认为渐显效果) 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; } } }
我记得以前用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上下载下来体验体验
开源库BaseRecyclerViewAdapterHelper
标签:
原文地址:http://blog.csdn.net/xiangzhihong8/article/details/52138669