RecyclerView佈局設定聊天介面
阿新 • • 發佈:2019-01-03
先上兩張效果圖:
——————————————————分割線————————————————————-
實體類
class Msg {
static final int TYPE_TIME = 0;
static final int TYPE_RECEIVE = 1;
static final int TYPE_SEND = 2;
String content;
int type;
Msg(String content, int type) {
this.content = content;
this .type = type;
}
}
Activity的佈局都是一樣
先說說第一張單一佈局的實現,適合介面比較簡單的時候使用
adapt
class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.MyViewHolder> {
private ArrayList<Msg> mMsgList;
MsgAdapter(ArrayList<Msg> mMsgList) {
this.mMsgList = mMsgList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.item_feedback_list, null);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder , int position) {
Msg msg = mMsgList.get(position);
if (msg.type == TYPE_TIME) {
holder.tv_time.setVisibility(View.VISIBLE);
holder.tv_receive.setVisibility(View.GONE);
holder.tv_send.setVisibility(View.GONE);
holder.tv_time.setText(msg.content);
} else if (msg.type == Msg.TYPE_RECEIVE) {
holder.tv_receive.setVisibility(View.VISIBLE);
holder.tv_time.setVisibility(View.GONE);
holder.tv_send.setVisibility(View.GONE);
holder.tv_receive.setText(msg.content);
} else {
holder.tv_send.setVisibility(View.VISIBLE);
holder.tv_receive.setVisibility(View.GONE);
holder.tv_time.setVisibility(View.GONE);
holder.tv_send.setText(msg.content);
}
}
@Override
public int getItemCount() {
return mMsgList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv_receive;
private TextView tv_time;
private TextView tv_send;
MyViewHolder(View itemView) {
super(itemView);
tv_receive = itemView.findViewById(R.id.tv_receive);
tv_time = itemView.findViewById(R.id.tv_time);
tv_send = itemView.findViewById(R.id.tv_send);
}
}
}
第二幅圖的adapter
class MsgAdapter extends RecyclerView.Adapter {
private Context mContext;
private ArrayList<Msg> mMsgList;
public MsgAdapter (Context context, ArrayList<Msg> msgList) {
mContext = context;
mMsgList = msgList;
}
@Override
public int getItemViewType(int position) {
return mMsgList.get(position).type;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_TIME) {
return new TimeViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_msg_time,parent,false));
} else if (viewType == TYPE_RECEIVE) {
return new ReceiveMsgViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_msg_receive,parent,false));
} else {
return new SendMsgViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_msg_send,parent,false));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof TimeViewHolder) {
((TimeViewHolder) holder).tv_msg_time.setText(mMsgList.get(position).content);
} else if (holder instanceof ReceiveMsgViewHolder) {
((ReceiveMsgViewHolder) holder).tv_receive_msg.setText(mMsgList.get(position).content);
} else {
((SendMsgViewHolder) holder).tv_send_msg.setText(mMsgList.get(position).content);
}
}
class SendMsgViewHolder extends RecyclerView.ViewHolder {
ImageView iv_send_face;
TextView tv_send_msg;
public SendMsgViewHolder(View itemView) {
super(itemView);
iv_send_face = itemView.findViewById(R.id.iv_send_face);
tv_send_msg = itemView.findViewById(R.id.tv_send_msg);
}
}
class ReceiveMsgViewHolder extends RecyclerView.ViewHolder {
ImageView iv_receive_face;
TextView tv_receive_msg;
public ReceiveMsgViewHolder(View itemView) {
super(itemView);
iv_receive_face = itemView.findViewById(R.id.iv_receive_face);
tv_receive_msg = itemView.findViewById(R.id.tv_receive_msg);
}
}
class TimeViewHolder extends RecyclerView.ViewHolder {
TextView tv_msg_time;
public TimeViewHolder(View itemView) {
super(itemView);
tv_msg_time = itemView.findViewById(R.id.tv_msg_time);
}
}
@Override
public int getItemCount() {
return mMsgList.size();
}
}