布局
<RelativeLayout android:id="@+id/listview_Re" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="10dp" android:layout_weight="3.5" android:background="@drawable/interfacerecy_bg" android:elevation="2dp"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_msglist" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
Activity: mAL_MsgList.add(new MsgBean("我想去吃饭", MsgBean.TYPE.SENT)); mAL_MsgList.add(new MsgBean("您好,您想吃些什么呢?", MsgBean.TYPE.RECEIVED)); mAL_MsgList.add(new MsgBean("我想吃火锅", MsgBean.TYPE.SENT)); mAL_MsgList.add(new MsgBean("我们这里的火锅店有很多,您想去哪一家呢?", MsgBean.TYPE.RECEIVED)); mAL_MsgList.add(new MsgBean("我想去海底捞", MsgBean.TYPE.SENT)); mAL_MsgList.add(new MsgBean("您好,您想吃些什么呢?", MsgBean.TYPE.RECEIVED)); mAL_MsgList.add(new MsgBean("我想吃火锅", MsgBean.TYPE.SENT)); mAL_MsgList.add(new MsgBean("我们这里的火锅店有很多,您想去哪一家呢?", MsgBean.TYPE.RECEIVED)); mAL_MsgList.add(new MsgBean("我想去海底捞", MsgBean.TYPE.SENT)); mRV_MsgList.setLayoutManager(new LinearLayoutManager(interFaceActivity)); MsgAdapter mMsgAdapter = new MsgAdapter(interFaceActivity,mAL_MsgList); mRV_MsgList.setAdapter(mMsgAdapter);
新消息:
//新内容 /*String text="新内容"; mAL_MsgList.add(new MsgBean(text, MsgBean.TYPE.SENT)); //如果有新消息,则设置适配器的长度(通知适配器,有新的数据被插入),并让 RecyclerView 定位到最后一行 int newSize = mAL_MsgList.size() - 1; mMsgAdapter.notifyItemInserted(newSize); mRV_MsgList.scrollToPosition(newSize);*/
消息bean:
public class MsgBean { /** * 内容 */ private String content; /** * 类型 */ private TYPE type; public enum TYPE{ /** * 接收 */ RECEIVED, /** * 发送 */ SENT } public MsgBean(String content,TYPE type){ this.content = content; this.type = type; } public TYPE getType() { return type; } public String getContent() { return content; } }
Adapter:
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> { private List<MsgBean> msgList; private Context context; static class ViewHolder extends RecyclerView.ViewHolder { LinearLayout leftLayout; LinearLayout rightLayout; TextView leftMsg; TextView rightMsg; SimpleDraweeView mSDV_ClientLogo; SimpleDraweeView mSDV_ServiceLogo; public ViewHolder(View itemView) { super(itemView); leftLayout = (LinearLayout) itemView.findViewById(R.id.left_layout); rightLayout = (LinearLayout) itemView.findViewById(R.id.right_layout); leftMsg = (TextView) itemView.findViewById(R.id.left_msg); rightMsg = (TextView) itemView.findViewById(R.id.right_msg); mSDV_ClientLogo = itemView.findViewById(R.id.image_clientlogo); mSDV_ServiceLogo = itemView.findViewById(R.id.image_servicelogo); } } public MsgAdapter(Context context , List<MsgBean> msgList) { this.msgList = msgList; this.context = context; } @Override public MsgAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(MsgAdapter.ViewHolder holder, int position) { MsgBean msg = msgList.get(position); switch (msg.getType()) { case RECEIVED://接收的消息 holder.mSDV_ServiceLogo.setImageURI(Uri.parse("res:///" + R.drawable.contact)); holder.leftLayout.setVisibility(View.VISIBLE); holder.rightLayout.setVisibility(View.GONE); holder.leftMsg.setText(msg.getContent()); break; case SENT://发出的消息 holder.mSDV_ClientLogo.setImageURI(Uri.parse("res:///" + R.drawable.contact)); holder.leftLayout.setVisibility(View.GONE); holder.rightLayout.setVisibility(View.VISIBLE); holder.rightMsg.setText(msg.getContent()); break; } } @Override public int getItemCount() { return msgList.size(); } }