BaseRecyclerViewAdapterHelper是一个强大并且灵活的RecyclerViewAdapter
在 build.gradle 的 repositories 添加:
allprojects { repositories { maven { url "https://jitpack.io" } } }
然后增加dependencies
dependencies { compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.5.8' }
public class QuickAdapter extends BaseQuickAdapter<Status> { public QuickAdapter(Context context) { super(context, 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()) .setImageUrl(R.id.tweetAvatar, item.getUserAvatar()) .setVisible(R.id.tweetRT, item.isRetweet()) .linkify(R.id.tweetText); } }
mQuickAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() { @Override public void onItemClick(View view, int position) { } });
// 一行代码搞定(默认为渐显效果) quickAdapter.openLoadAnimation();
另外也可以制定其他的动画效果
// 默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左) // ALPHAIN, SCALEIN, SLIDEIN_BOTTOM, SLIDEIN_LEFT, SLIDEIN_RIGHT 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) }; } });
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem> { public MultipleItemQuickAdapter(Context context, List data) { super(context, data); addItmeType(MultipleItem.TEXT, R.layout.text_view); addItmeType(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.addHeaderView(getView()); mQuickAdapter.addFooterView(getView());
mQuickAdapter.setOnLoadMoreListener(PAGE_SIZE, new BaseQuickAdapter.RequestLoadMoreListener() { @Override public void onLoadMoreRequested() { if (mCurrentCounter >= TOTAL_COUNTER) { mRecyclerView.post(new Runnable() { @Override public void run() { mQuickAdapter.isNextLoad(false); } }); } else { // reqData mCurrentCounter = mQuickAdapter.getItemCount(); mQuickAdapter.isNextLoad(true); } } });
public class SectionAdapter extends BaseSectionQuickAdapter<MySection> { public SectionAdapter(Context context, int layoutResId, int sectionHeadResId, List data) { super(context, 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); if(!item.isMroe)helper.setVisible(R.id.more,false); 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(); } }); }
mQuickAdapter.setEmptyView(getView());
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(); } });
博主按:本文原始稿件系网友投稿,经本人修改而成。技术小黑屋接受并欢迎网友投稿,对于每一份稿件,我都会请自审阅并作出修改。欢迎大家投稿。