在AdapterView和RecyclerView中通用的Adapter和ViewHolder,使AdapterView和RecyclerView适配器的使用方式基本一致。
使用非常简单,这里展示一下ListView和RecyclerView实现qq聊天界面的适配器
public class ListChatAdapter extends BGAAdapterViewAdapter<ChatModel> {
public ListChatAdapter(Context context) {
super(context, R.layout.item_chat);
}
/**
* 为item的孩子节点设置监听器,并不是每一个数据列表都要为item的子控件添加事件监听器,所以在父类中采用了空实现,需要设置事件监听器时重写该方法即可
*
* @param viewHolderHelper
*/
@Override
protected void setItemChildListener(BGAViewHolderHelper viewHolderHelper) {
}
@Override
public void fillData(BGAViewHolderHelper viewHolderHelper, int position, ChatModel model) {
if (model.mUserType == ChatModel.UserType.From) {
viewHolderHelper.setVisibility(R.id.rl_item_chat_to, View.GONE);
viewHolderHelper.setVisibility(R.id.rl_item_chat_from, View.VISIBLE);
String htmlMsg = String.format(mContext.getString(R.string.color_msg_from), model.mMsg);
viewHolderHelper.setHtml(R.id.tv_item_chat_from_msg, htmlMsg);
} else {
viewHolderHelper.setVisibility(R.id.rl_item_chat_from, View.GONE);
viewHolderHelper.setVisibility(R.id.rl_item_chat_to, View.VISIBLE);
String htmlMsg = String.format(mContext.getString(R.string.color_msg_to), model.mMsg);
viewHolderHelper.setHtml(R.id.tv_item_chat_to_msg, htmlMsg);
}
}
}
RecyclerView实现qq聊天界面的适配器
public class RecyclerChatAdapter extends BGARecyclerViewAdapter<ChatModel> {
public RecyclerChatAdapter(RecyclerView recyclerView) {
super(recyclerView, R.layout.item_chat);
}
/**
* 为item的孩子节点设置监听器,并不是每一个数据列表都要为item的子控件添加事件监听器,所以在父类中采用了空实现,需要设置事件监听器时重写该方法即可
*
* @param viewHolderHelper
*/
@Override
public void setItemChildListener(BGAViewHolderHelper viewHolderHelper) {
}
@Override
public void fillData(BGAViewHolderHelper viewHolderHelper, int position, ChatModel model) {
if (model.mUserType == ChatModel.UserType.From) {
viewHolderHelper.setVisibility(R.id.rl_item_chat_to, View.GONE);
viewHolderHelper.setVisibility(R.id.rl_item_chat_from, View.VISIBLE);
String htmlMsg = String.format(mContext.getString(R.string.color_msg_from), model.mMsg);
viewHolderHelper.setHtml(R.id.tv_item_chat_from_msg, htmlMsg);
} else {
viewHolderHelper.setVisibility(R.id.rl_item_chat_from, View.GONE);
viewHolderHelper.setVisibility(R.id.rl_item_chat_to, View.VISIBLE);
String htmlMsg = String.format(mContext.getString(R.string.color_msg_to), model.mMsg);
viewHolderHelper.setHtml(R.id.tv_item_chat_to_msg, htmlMsg);
}
}
}
运行效果:
效果源码:http://www.itlanbao.com/code/20151111/10000/100639.html