本文是借鉴bingoogolapple写得BGAAdapter-Android而产生的,对此表示感谢。

效果

1.Adapter的使用

1.继承BaseAdapter

这里是我的adapter

public class RecyclerChatAdapter extends BaseAdapter<ChatModel> {
public Context context;
protected ADUholder holder; public RecyclerChatAdapter(Context context) {
super();
this.context = context;
} @Override
public int getItemViewType(int position) {
return mDatas.get(position).type;
} @Override
protected BaseViewHolder addViewHolder(ViewGroup parent, int viewType) {
View view = null;
switch (viewType) {
case Type.chat:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat, parent, false);
return new ChatHolder(view);
case Type.other:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
return new ADUholder(view);
default://防止空指针
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat, parent, false);
return new ChatHolder(view);
}
} @Override
protected void fillData(RecyclerView.ViewHolder holder, int position, ChatModel model) {
if (holder instanceof ChatHolder) {
ChatHolder chatHolder = (ChatHolder) holder;
chatHolder.fillData(context, mDatas, position);
} else if (holder instanceof ADUholder) {
ADUholder adUholder = (ADUholder) holder;
adUholder.fillData(context, mDatas, position);
}
}
}

注意这里要实现多item布局就要重写getItemViewType()方法,我们可以在item中的属性中增加一个类型,如我这里是在ChatModel中增加了一个int type;在getItemViewType()获取它。

2.重写addViewHolder()

根据参数viewType来加载不同的item布局,我这里仅仅写了两种,你可以继续增加。

@Override
protected BaseViewHolder addViewHolder(ViewGroup parent, int viewType) {
View view = null;
switch (viewType) {
case Type.chat:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat, parent, false);
return new ChatHolder(view);
case Type.other:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
return new ADUholder(view);
}
return null;
}

3.重写getItemViewType()

代码如下:

@Override
public int getItemViewType(int position) {
return mDatas.get(position).type;
}

这里根据你每个item中的type来返回,对应addViewHolder()方法中的参数viewType。

4.重写fillData()

这里就是填充数据了

@Override
protected void fillData(RecyclerView.ViewHolder holder, int position, ChatModel model) {
if (holder instanceof ChatHolder) {
ChatHolder chatHolder = (ChatHolder) holder;
chatHolder.fillData(context, mDatas, position);
} else if (holder instanceof ADUholder) {
ADUholder adUholder = (ADUholder) holder;
adUholder.fillData(context, mDatas, position);
}
}

adapter这里我们就做完了,剩下了就是ViewHolder。

2.ViewHolder的使用

1.继承BaseViewHolder

我的代码如下:

public class ChatHolder extends BaseViewHolder<ChatModel> {
protected RelativeLayout rl_item_chat_to;
protected RelativeLayout rl_item_chat_from;
protected TextView tv_item_chat_from_msg;
protected TextView tv_item_chat_to_msg; public ChatHolder(View view) {
super(view);
rl_item_chat_to = findViewById(R.id.rl_item_chat_to);
rl_item_chat_from = findViewById(R.id.rl_item_chat_from);
tv_item_chat_from_msg = findViewById(R.id.tv_item_chat_from_msg);
tv_item_chat_to_msg = findViewById(R.id.tv_item_chat_to_msg);
} @Override
public void fillData(Context context, List<ChatModel> datas, int position) {
ChatModel model = datas.get(position);
if (model.mUserType == ChatModel.UserType.From) {
rl_item_chat_to.setVisibility(View.GONE);
rl_item_chat_from.setVisibility(View.VISIBLE);
String msg = String.format(mContent.getString(R.string.color_msg_from), model.mMsg);
Spanned htmlMsg = Html.fromHtml(msg);
tv_item_chat_from_msg.setText(htmlMsg,TextView.BufferType.SPANNABLE);
} else {
rl_item_chat_to.setVisibility(View.VISIBLE);
rl_item_chat_from.setVisibility(View.GONE);
String msg = String.format(mContent.getString(R.string.color_msg_from), model.mMsg);
Spanned htmlMsg = Html.fromHtml(msg);
tv_item_chat_to_msg.setText(htmlMsg,TextView.BufferType.SPANNABLE);
}
}
}

2.构造方法

你要实现一个带有View参数的构造方法,我们可以在此做控件绑定。

3.重写fillData()

这里是你真正控件填充数据的地方,对每个item项。

4.另一个ViewHolder

public class ADUholder extends BaseViewHolder<ChatModel> {
public ImageView imageView;
public TextView title;
public TextView name; public ADUholder(View itemView) {
super(itemView);
imageView = findViewById(R.id.imageView);
title = findViewById(R.id.tv_title);
name = findViewById(R.id.tv_name);
} @Override
public void fillData(Context context, List<ChatModel> datas, int position) {
ChatModel model = datas.get(position);
title.setText(model.name + position);
name.setText(model.mMsg);
} }

是不是感觉简单了很多,当然这是我自己的看法。

好了,在此附上github源码,喜欢的请start、fork。 https://github.com/DyncKathline/TestRecyclerView

转载请注明出处,谢谢!

对RecycleView的多种item布局的封装的更多相关文章

  1. Android RecycleView实现混合Item布局

    首先来看看效果吧: 效果预览.png 本实例来自于慕课网的视屏http://www.imooc.com/video/13046,实现步骤可以自己去观看视屏,这里只记录了下实现的代码. 添加依赖: (1 ...

  2. ListView具有多种item布局——实现微信对话列

    这篇文章的效果也是大家常见的,各种通讯应用的对话列表都是这种方式,像微信.whatsapp.易信.米聊等.我们这篇文章也权当为回忆,形成简单的笔记.这篇文章参考了2009年Google IO中的< ...

  3. ListView实现多种item布局的方法和注意事项

    这篇文章的效果也是大家常见的,各种通讯应用的对话列表都是这种方式,像微信.whatsapp.易信.米聊等.我们这篇文章也权当为回忆,形成简单的笔记.这篇文章参考了2009年Google IO中的< ...

  4. RecyclerView的使用之多种Item加载布局

    精益求精,为了更加透彻熟练得掌握,本文再次给大家介石介绍下如何利用RecyclerView实现多Item布局的加载,多Item布局的加载的意思就是在开发过程中List的每一项可能根据需求的不同会加载不 ...

  5. ListView 完全优化 + 多种listitem布局处理

    #  百度了下,感觉下面的博客文章还都挺全面的,写的很好,直接分享得了 Android性能优化--Listview优化 - tonycheng93 - 博客园http://www.cnblogs.co ...

  6. RecyclerView的使用(2)之多Item布局的载入

    原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50708349 李济洲的博客 上一篇介绍的了RecyclerView的基础使用ht ...

  7. 2.Android 自定义通用的Item布局

    转载:http://www.jianshu.com/p/e7ba4884dcdd BaseItemLayout 简介 在工作中经常会遇到下面的一些布局,如图标红处: 05.png 07.png 08. ...

  8. 【转】Android ListView加载不同的item布局

    原创教程,转载请保留出处:http://www.eoeandroid.com/thread-72369-1-1.html     最近有需求需要在listView中载入不同的listItem布局,开始 ...

  9. RecyclerView实现一个页面有多种item,每个item有多个view,并且可以让任意item的任意view自定义监听,通过接口方法进行触发操作

    百度了很多贴子,看着大佬的博客,模仿尝试,最终都是以失败告终,api可能版本不一样, 毕竟博客大佬都是7~8前写的,日期新点的都是好几年前了,多次尝试,还是报出莫名其妙的错. 哎,忧伤. 翻阅各种资料 ...

随机推荐

  1. 数据库水平拆分和垂直拆分区别(以mysql为例)

    数据库水平拆分和垂直拆分区别(以mysql为例) 数据库水平拆分和垂直拆分区别(以mysql为例)   案例:     简单购物系统暂设涉及如下表: 1.产品表(数据量10w,稳定) 2.订单表(数据 ...

  2. Mysql table ful

    http://ourmysql.com/archives/1327 http://blog.csdn.net/kevon_sun/article/details/7967728 http://my.o ...

  3. jquery 提示插件 cluetip

    jquery的 插件cluetip, 地址下载是:plugins.learningjquery.com/cluetip/demo/ 下面简单讲解下用法: 1 首先当然要放JQUERY的基本JS,和这个 ...

  4. C#功能扩张方式

    有时候,为了完成一些window的一些操作,需要引入一些dll进行操作 1)  引入系统api进行操作,形如[DllImport("urlmon.dll", CharSet = C ...

  5. 2.linux下Makefile编写规范

    转自陈皓 (CSDN) 概述—— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 profession ...

  6. JAVA用geotools读写shape格式文件

    转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...

  7. HDU- Who Gets the Most Candies?

    Who Gets the Most Candies? Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 262144/131072K (J ...

  8. 在Apache上部署Django

    跟php类似,要想使Apache能够处理python脚本(实际上Apache是不能处理脚本的,只是利用别的扩展模块处理相应脚本),就要给Apache安装相应的扩展模块. Apache起初的python ...

  9. WCF性能优势体现 【转】

    WCF性能优势决定了其受欢迎程度,这些优势主要都体现在:统一性:互操作性:安全与可信赖:兼容性等方面. WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架. ...

  10. 如何扩展大规模Web网站的性能?

    Reduce Data广告服务网站如何扩展到每天300K QPS请求?分享经验如下: 1. 为大规模设计,广告服务平台从一开始增长就很惊人,因此,系统开始就为大规模设计,系统为水平和垂直伸缩扩展. 2 ...