Android推荐几款好用的开源作品二之万能RecyclerView适配器讲解.docx
- 文档编号:10042556
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:20
- 大小:113.60KB
Android推荐几款好用的开源作品二之万能RecyclerView适配器讲解.docx
《Android推荐几款好用的开源作品二之万能RecyclerView适配器讲解.docx》由会员分享,可在线阅读,更多相关《Android推荐几款好用的开源作品二之万能RecyclerView适配器讲解.docx(20页珍藏版)》请在冰豆网上搜索。
Android推荐几款好用的开源作品二之万能RecyclerView适配器讲解
Android推荐几款好用的开源作品
(二)之万能RecyclerView适配器
前言:
RecyclerView出现已经有一段时间了,我们肯定不陌生了,可以通过导入support-v7对其进行使用。
根据官方的文档,该控件用于在有限的窗口中展示大量数据集,其实有了它就可以替代ListView、GridView了。
了解RecyclerView架构,可以高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration,ItemAnimator可以实现的想要的效果。
1、首先介绍几种LayoutManager
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
LinearLayoutManagermManager=newLinearLayoutManager(mContent);
LinearLayoutManagermManager=newLinearLayoutManager(mContent,LinearLayoutManager.VERTICAL,false);//垂直布局,true表示左右翻转,false不翻转
GridLayoutManagermManager=newGridLayoutManager(mContent,3);
GridLayoutManagermManager=newGridLayoutManager(mContent,3,LinearLayoutManager.HORIZONTAL,false);//每行3列,水平,true表示左右翻转,false不翻转
mRecyclerView.setLayoutManager(mManager);
2、RecycleView简单使用
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
//初始化控件
mRecyclerView=findView(R.id.id_recyclerview);
//设置布局管理器
mManager=newLinearLayoutManager(mContent);
mRecyclerView.setLayoutManager(mManager);
//设置adapter
mRecyclerView.setAdapter(mAdapter)
//设置Item增加、移除动画
mRecyclerView.setItemAnimator(newDefaultItemAnimator());
//添加分割线
mRecyclerView.addItemDecoration(newDividerItemDecoration(
getActivity(),DividerItemDecoration.HORIZONTAL_LIST));
3、接下来介绍的就是RecyclerView的适配器了,同样,RecyclerView和ListView,GridView一样需要adapter来填充数据,同样,自定义一个adapter需要继承RecyclerView.Adapter,代码需要实现几个父类方法onCreateViewHolder,onBindViewHolder,getItemCount(),根据名字就能知道:
(1)onCreateViewHolder:
主要返回的是拿到布局,进而返回ViewHolder
returnnewViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell,null));
(2)onBindViewHolder:
拿到ViewHolder中的控件对其赋值操作
(3)getItemCount():
返回数组的size()
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
publicclassMyAdapterextendsRecyclerView.Adapter{
classViewHolderextendsRecyclerView.ViewHolder{
TextViewtvTitle,tvContent;
publicViewHolder(ViewitemView){
super(itemView);
tvTitle=(TextView)itemView.findViewById(R.id.tv_title);
tvContent=(TextView)itemView.findViewById(R.id.tv_content);
}
}
@Override
publicRecyclerView.ViewHolderonCreateViewHolder(ViewGroupparent,intviewType){
returnnewViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell,null));
}
@Override
publicvoidonBindViewHolder(RecyclerView.ViewHolderholder,intposition){
ViewHoldervh=(ViewHolder)holder;
CellDatacd=data[position];
vh.tvTitle.setText(cd.title);
vh.tvContent.setText(cd.content);
}
@Override
publicintgetItemCount(){
returndata.length;
}
privateCellData[]data=newCellData[]{newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错"),newCellData("recycleview","学习recycleview"),newCellData("天气","今天天气不错")};
}
4、效果图,虽然很难看,但是是基本的实现方式。
可见,RecyclerView和传统的ListView,GridView使用的流程是一样的,但是由于RecyclerView的高定制性,使用者越来越多,已经有大部分的开发者放弃使用ListView和GridView了。
想要用好RecyclerView也不难,关键就在于Adapter的书写上,接下来介绍一个非常好用的RecyclerView万能适配器(BaseRecyclerViewAdapterHelper),前几天接触到的,感觉十分好用,所包含的功能有以下几个(官网介绍):
优化Adapter代码(减少百分之70%代码)
添加点击item点击、长按事件、以及item子控件的点击事件
添加加载动画(一行代码轻松切换5种默认动画)
添加头部、尾部、下拉刷新、上拉加载(感觉又回到ListView时代)
设置自定义的加载更多布局
添加分组(随心定义分组头部)
自定义不同的item类型(简单配置、无需重写额外方法)
设置空布局(比Listview的setEmptyView还要好用!
)
添加拖拽item
1、添加item点击、长按事件
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
mQuickAdapter.setOnRecyclerViewItemClickListener();
mQuickAdapter.setOnRecyclerViewItemLongClickListener();
2、新增添加子布局多个控件的点击事件
Adapter
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
protectedvoidconvert(BaseViewHolderhelper,Statusitem){
helper.setOnClickListener(R.id.tweetAvatar,newOnItemChildClickListener())
.setOnClickListener(R.id.tweetName,newOnItemChildClickListener());
}
Activity
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
mQuickAdapter.setOnRecyclerViewItemChildClickListener(newBaseQuickAdapter.OnRecyclerViewItemChildClickListener(){
@Override
publicvoidonItemChildClick(BaseQuickAdapteradapter,Viewview,intposition){
Stringcontent=null;
Statusstatus=(Status)adapter.getItem(position);
switch(view.getId()){
caseR.id.tweetAvatar:
content="img:
"+status.getUserAvatar();
break;
caseR.id.tweetName:
content="name:
"+status.getUserName();
break;
}
Toast.makeText(AnimationUseActivity.this,content,Toast.LENGTH_LONG).show();
}
});
3、添加动画
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
//一行代码搞定(默认为渐显效果)
quickAdapter.openLoadAnimation();
//默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左)
quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//自定义动画如此轻松
quickAdapter.openLoadAnimation(newBaseAnimation(){
@Override
publicAnimator[]getAnimators(Viewview){
returnnewAnimator[]{
ObjectAnimator.ofFloat(view,"scaleY",1,1.1f,1),
ObjectAnimator.ofFloat(view,"scaleX",1,1.1f,1)
};
}
});
4、添加头部和尾部
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
mQuickAdapter.addHeaderView(getView());
mQuickAdapter.addFooterView(getView());
5、上拉加载更多
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
mQuickAdapter.openLoadMore(PAGE_SIZE,true);//必须设置pageSize,否则上拉不会加载,pageSize一般为每次请求需要加载的数据条数
mQuickAdapter.setOnLoadMoreListener(newBaseQuickAdapter.RequestLoadMoreListener(){
@Override
publicvoidonLoadMoreRequested(){
mRecyclerView.post(newRunnable(){
@Override
publicvoidrun(){
if(mCurrentCounter>=TOTAL_COUNTER){
mQuickAdapter.notifyDataChangedAfterLoadMore(false);
}else{
mQuickAdapter.notifyDataChangedAfterLoadMore(DataServer.getSampleData(PAGE_SIZE),true);
mCurrentCounter=mQuickAdapter.getItemCount();
}
}
});
}
});
可以通过mQuickAdapter.setLoadingView(customView);自定义加载更多的布局
6、使用分组
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
publicclassSectionAdapterextendsBaseSectionQuickAdapter
publicSectionAdapter(intlayoutResId,intsectionHeadResId,Listdata){
super(layoutResId,sectionHeadResId,data);
}
@Override
protectedvoidconvert(BaseViewHolderhelper,MySectionitem){
helper.setImageUrl(R.id.iv,(String)item.t);
}
@Override
protectedvoidconvertHead(BaseViewHolderhelper,finalMySectionitem){
helper.setText(R.id.header,item.header);
else
helper.setOnClickListener(R.id.more,newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
Toast.makeText(context,item.header+"more..",Toast.LENGTH_LONG).show();
}
});
}
7、添加多种类型Item
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
publicclassMultipleItemQuickAdapterextendsBaseMultiItemQuickAdapter
publicMultipleItemQuickAdapter(Listdata){
super(data);
addItemType(MultipleItem.TEXT,R.layout.text_view);
addItemType(MultipleItem.IMG,R.layout.image_view);
}
@Override
protectedvoidconvert(BaseViewHolderhelper,MultipleItemitem){
switch(helper.getItemViewType()){
caseMultipleItem.TEXT:
helper.setImageUrl(R.id.tv,item.getContent());
break;
caseMultipleItem.IMG:
helper.setImageUrl(R.id.iv,item.getContent());
break;
}
}
}
8、使用setEmptyView
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
mQuickAdapter.setEmptyView(getView());
9、使用拖拽与滑动删除
[java]viewplaincopy在CODE上查看代码片派生到我的代码片
OnItemDragListeneronItemDragListener=newOnItemDragListener(){
@Override
publicvoidonItemDragStart(RecyclerView.ViewHolderviewHolder,intpos){}
@Override
publicvoidonItemDragMoving(RecyclerView.ViewHoldersource,intfrom,RecyclerView.ViewHoldertarget,intto){}
@Override
publicvoidonItemDragEnd(RecyclerView.ViewHolderviewHolder,intpos){}
}
OnItemSwipeListeneronItemSwipeListener=newOnItemSwipeListener(){
@Override
publicvoidonItemSwipeStart(RecyclerView.ViewHolderviewHolder,intpos){}
@Override
publicvoidclearView(RecyclerView.ViewHolderviewHolder,intpos){}
@Override
publicvoidonItemSwiped(RecyclerView.ViewHolderviewHolder,intpos){}
};
ItemDragAndSwipeCallbackitemDragAndSwipeCallback=newItemDragAndSwipeCallback(mAdapter);
ItemTouchHelperitemTouchHelper=newItemTouchHelper(itemDragAndSwipeCal
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 推荐 几款好用 作品 万能 RecyclerView 适配器 讲解
![提示](https://static.bdocx.com/images/bang_tan.gif)