知识点:
1、使用了两个ViewHolder,等于有两个不同的item
2、RecyclerView.ViewHolder 与 自定义的 MeViewHolder 的使用。
注意MeViewHolder 与 RecyclerView.ViewHolder 的使用。
如果public class ChatDetailAdapter extends RecyclerView.Adapter
继承时写的是父类,那么下面方法也是使用父类,使用前注意转换,例如:(MeViewHolder) holder
public void onBindViewHolder(RecyclerView.ViewHolder holder
3、getItemViewType
------------------------------------- 原文 -----------------------------
实现多种item有许多种方式,我们这里就用RecyclerView提供给我们的方式,getItemViewType。
这个方法是RecyclerView专门开放给我们用来实现多item类型的方法,所以他是最优雅的。
我仔细看过源码,这种方式用来实现多种Item,绝对是丝毫不影响性能的,请放心使用。
以即时通讯app为例子,你肯定要有一左一右的对话框吧。
所以在Adapter中先定义这两种ViewHolder。
private static class MeViewHolder extends RecyclerView.ViewHolder {
TextView mTvMyMsg;
ImageView mIvMyAvatar;
public MeViewHolder(View view) {
super(view);
mTvMyMsg = (TextView) view.findViewById(R.id.tv_my_msg);
mIvMyAvatar = (ImageView) view.findViewById(R.id.iv_my_avatar);
}
}
private static class FriendViewHolder extends RecyclerView.ViewHolder {
TextView mTvFriendMsg;
ImageView mIvFriendAvatar;
public FriendViewHolder(View view) {
super(view);
mTvFriendMsg = (TextView) view.findViewById(R.id.tv_friend_msg);
mIvFriendAvatar = (ImageView) view.findViewById(R.id.iv_friend_avatar);
}
}
getItemViewType方法,我这里list装的是bean,每个bean里都有一个,0代表我自己的对话,1代表对方的对话。
@Override
public int getItemViewType(int position) {
return list.get(position).getMsgType();
}
onCreateViewHolder方法,一目了然
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (context == null) {
context = parent.getContext();
}
if (viewType == TYPE_ME) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_chat_me, parent, false);
return new MeViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_chat_friend, parent, false);
return new FriendViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MeViewHolder) {
((MeViewHolder) holder).mTvMyMsg.setText(list.get(position).getMsgContent());
} else {
((FriendViewHolder) holder).mTvFriendMsg.setText(list.get(position).getMsgContent());
}
}
效果图:(我这里是点击添加一个对话,点击添加一个,即时通讯这里先不实现)
全部代码
package com.example.wechat.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.wechat.R;
import com.example.wechat.bean.ChatDetailBean;
import com.example.wechat.bean.ChatItemBean;
import com.example.wechat.chat.ChatActivity;
import java.util.List;
import static com.example.wechat.R.id.view;
public class ChatDetailAdapter extends RecyclerView.Adapter {
private List list;
public ChatDetailAdapter(List list) {
this.list = list;
}
private Context context = null;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (context == null) {
context = parent.getContext();
}
if (viewType == TYPE_ME) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_chat_me, parent, false);
return new MeViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_chat_friend, parent, false);
return new FriendViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MeViewHolder) {
((MeViewHolder) holder).mTvMyMsg.setText(list.get(position).getMsgContent());
} else {
((FriendViewHolder) holder).mTvFriendMsg.setText(list.get(position).getMsgContent());
}
}
private static final int TYPE_ME = 0;
private static final int TYPE_FRIEND = 1;
@Override
public int getItemViewType(int position) {
return list.get(position).getMsgType();
}
@Override
public int getItemCount() {
return list.size();
}
private static class MeViewHolder extends RecyclerView.ViewHolder {
TextView mTvMyMsg;
ImageView mIvMyAvatar;
public MeViewHolder(View view) {
super(view);
mTvMyMsg = (TextView) view.findViewById(R.id.tv_my_msg);
mIvMyAvatar = (ImageView) view.findViewById(R.id.iv_my_avatar);
}
}
private static class FriendViewHolder extends RecyclerView.ViewHolder {
TextView mTvFriendMsg;
ImageView mIvFriendAvatar;
public FriendViewHolder(View view) {
super(view);
mTvFriendMsg = (TextView) view.findViewById(R.id.tv_friend_msg);
mIvFriendAvatar = (ImageView) view.findViewById(R.id.iv_friend_avatar);
}
}
}
---------------------
作者:fvcasdgva
来源:CSDN
原文:https://blog.csdn.net/qq_36523667/article/details/78844312
版权声明:本文为博主原创文章,转载请附上博文链接!