1、Android 推荐几款好用的开源作品二之万能RecyclerView适配器Android 推荐几款好用的开源作品(二)之万能RecyclerView适配器前言:RecyclerView出现已经有一段时间了,我们肯定不陌生了,可以通过导入support-v7对其进行使用。根据官方的文档,该控件用于在有限的窗口中展示大量数据集,其实有了它就可以替代ListView、GridView了。了解RecyclerView架构,可以高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator可以实现的想要的效果。1、首先介绍几种Layo
2、utManagerjava view plain copy 在CODE上查看代码片派生到我的代码片LinearLayoutManager mManager = new LinearLayoutManager(mContent); LinearLayoutManager mManager = new LinearLayoutManager(mContent, LinearLayoutManager.VERTICAL, false);/垂直布局,true表示左右翻转,false不翻转 GridLayoutManager mManager = new GridLayoutManager(mConte
3、nt,3); GridLayoutManager mManager = new GridLayoutManager(mContent,3,LinearLayoutManager.HORIZONTAL,false);/每行3列,水平,true表示左右翻转,false不翻转 mRecyclerView.setLayoutManager(mManager); 2、RecycleView简单使用java view plain copy 在CODE上查看代码片派生到我的代码片/初始化控件 mRecyclerView = findView(R.id.id_recyclerview); /设置布局管理器 m
4、Manager = new LinearLayoutManager(mContent); mRecyclerView.setLayoutManager(mManager); /设置adapter mRecyclerView.setAdapter(mAdapter) /设置Item增加、移除动画 mRecyclerView.setItemAnimator(new DefaultItemAnimator(); /添加分割线 mRecyclerView.addItemDecoration(new DividerItemDecoration( getActivity(), DividerItemDec
5、oration.HORIZONTAL_LIST); 3、接下来介绍的就是RecyclerView的适配器了,同样,RecyclerView和ListView,GridView一样需要adapter来填充数据,同样,自定义一个adapter需要继承RecyclerView.Adapter,代码需要实现几个父类方法onCreateViewHolder,onBindViewHolder,getItemCount(),根据名字就能知道:(1)onCreateViewHolder:主要返回的是拿到布局,进而返回ViewHolder return new ViewHolder(LayoutInflater
6、.from(parent.getContext().inflate(R.layout.list_cell,null);(2)onBindViewHolder:拿到ViewHolder中的控件对其赋值操作(3)getItemCount():返回数组的size()java view plain copy 在CODE上查看代码片派生到我的代码片public class MyAdapter extends RecyclerView.Adapter class ViewHolder extends RecyclerView.ViewHolder TextView tvTitle,tvContent; p
7、ublic ViewHolder(View itemView) super(itemView); tvTitle = (TextView) itemView.findViewById(R.id.tv_title); tvContent = (TextView) itemView.findViewById(R.id.tv_content); Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) return new ViewHolder(LayoutInflater.f
8、rom(parent.getContext().inflate(R.layout.list_cell,null); Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) ViewHolder vh = (ViewHolder) holder; CellData cd = dataposition; vh.tvTitle.setText(cd.title); vh.tvContent.setText(cd.content); Override public int getItemCo
9、unt() return data.length; private CellData data = new CellDatanew CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new Ce
10、llData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学
11、习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new Ce
12、llData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellDa
13、ta(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习rec
14、ycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错),new CellData(recycleview,学习recycleview),new CellData(天气,今天天气不错); 4、效果图,虽然很
15、难看,但是是基本的实现方式。可见,RecyclerView和传统的ListView,GridView使用的流程是一样的,但是由于RecyclerView的高定制性,使用者越来越多,已经有大部分的开发者放弃使用ListView和GridView了。想要用好RecyclerView也不难,关键就在于Adapter的书写上,接下来介绍一个非常好用的RecyclerView万能适配器(BaseRecyclerViewAdapterHelper),前几天接触到的,感觉十分好用,所包含的功能有以下几个(官网介绍):优化Adapter代码(减少百分之70%代码)添加点击item点击、长按事件、以及item子
16、控件的点击事件添加加载动画(一行代码轻松切换5种默认动画)添加头部、尾部、下拉刷新、上拉加载(感觉又回到ListView时代)设置自定义的加载更多布局添加分组(随心定义分组头部)自定义不同的item类型(简单配置、无需重写额外方法)设置空布局(比Listview的setEmptyView还要好用!)添加拖拽item1、添加item点击、长按事件java view plain copy 在CODE上查看代码片派生到我的代码片mQuickAdapter.setOnRecyclerViewItemClickListener(); mQuickAdapter.setOnRecyclerViewItem
17、LongClickListener(); 2、新增添加子布局多个控件的点击事件Adapterjava view plain copy 在CODE上查看代码片派生到我的代码片protected void convert(BaseViewHolder helper, Status item) helper.setOnClickListener(R.id.tweetAvatar, new OnItemChildClickListener() .setOnClickListener(R.id.tweetName, new OnItemChildClickListener(); Activityjava
18、 view plain copy 在CODE上查看代码片派生到我的代码片mQuickAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() Override public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) String content = null; Status status = (Status) adapter.getIt
19、em(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(); ); 3、添加动画java view plain copy 在CODE上查看代码片派生到我的代码
20、片/ 一行代码搞定(默认为渐显效果) quickAdapter.openLoadAnimation(); / 默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左) quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN); / 自定义动画如此轻松 quickAdapter.openLoadAnimation(new BaseAnimation() Override public Animator getAnimators(View view) return new Animator ObjectAnimator.ofFloat(v
21、iew, scaleY, 1, 1.1f, 1), ObjectAnimator.ofFloat(view, scaleX, 1, 1.1f, 1) ; ); 4、添加头部和尾部java view plain copy 在CODE上查看代码片派生到我的代码片mQuickAdapter.addHeaderView(getView(); mQuickAdapter.addFooterView(getView(); 5、上拉加载更多java view plain copy 在CODE上查看代码片派生到我的代码片mQuickAdapter.openLoadMore(PAGE_SIZE, true);/
22、必须设置pageSize,否则上拉不会加载,pageSize一般为每次请求需要加载的数据条数 mQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() Override public void onLoadMoreRequested() mRecyclerView.post(new Runnable() Override public void run() if (mCurrentCounter = TOTAL_COUNTER) mQuickAdapter.notifyDataChang
23、edAfterLoadMore(false); else mQuickAdapter.notifyDataChangedAfterLoadMore(DataServer.getSampleData(PAGE_SIZE), true); mCurrentCounter = mQuickAdapter.getItemCount(); ); ); 可以通过mQuickAdapter.setLoadingView(customView);自定义加载更多的布局6、使用分组java view plain copy 在CODE上查看代码片派生到我的代码片public class SectionAdapter
24、 extends BaseSectionQuickAdapter 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 convertHea
25、d(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(); ); 7、添加多种类型Itemjava view plain copy 在C
26、ODE上查看代码片派生到我的代码片public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter 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 help
27、er, 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; 8、使用setEmptyViewjava view plain copy 在CODE上查看代码片派生到我的代码片mQuickAdapter.setEmptyView(getVie
28、w(); 9、使用拖拽与滑动删除java view plain copy 在CODE上查看代码片派生到我的代码片OnItemDragListener onItemDragListener = new OnItemDragListener() Override public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) Override public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.View
29、Holder 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(itemDragAndSwipeCall
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1