对RecycleView的多种item布局的封装

时间:2024-09-23 10:06:32

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

效果

对RecycleView的多种item布局的封装

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

转载请注明出处,谢谢!