1.思路
通过RecyclerView的适配器RecyclerView.Adapter<RecyclerView.ViewHolder>中的getItemViewType(int position)方法判断是什么类型,然后加载相应的布局,通过当前聊天时间和上一个item的聊天时间作对比,如果时间大于一分钟则显示聊天时间,小于则不显示。
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fl_title" android:layout_width="match_parent" android:layout_height="@dimen/_50dp" android:background="@color/white"> <FrameLayout android:id="@+id/fl_back" android:layout_width="@dimen/normal_200dp" android:layout_height="match_parent" android:layout_gravity="left|center_vertical"> <TextView android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/normal_20dp" android:background="@mipmap/arrow_right" /> </FrameLayout> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#333333" android:text="客服" android:textSize="@dimen/textSize_16" /> <FrameLayout android:id="@+id/fl_right" android:layout_width="@dimen/normal_160dp" android:layout_height="match_parent" android:layout_gravity="center_vertical|right"> <TextView android:id="@+id/tv_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:layout_marginRight="@dimen/normal_20dp" android:textColor="@color/textColor" android:textSize="@dimen/normal_30sp" /> </FrameLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/normal_2dp" android:layout_gravity="bottom" android:background="@color/divider" /> </FrameLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView_chat" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="50dp" android:layout_weight="1" android:layout_below="@id/fl_title" android:background="#fff" android:overScrollMode="never" android:scrollbars="vertical" /> <LinearLayout android:id="@+id/liear" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <EditText android:id="@+id/editText_message" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="7" android:layout_margin="@dimen/_7dp" android:cursorVisible="false" android:paddingLeft="@dimen/_10dp" android:hint="请输入消息内容" android:background="@drawable/send_edittext" /> <TextView android:id="@+id/btn_send" android:gravity="center" android:layout_weight="1" android:layout_margin="@dimen/_5dp" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="@color/white" android:background="@drawable/send_textview" android:text="发送" /> </LinearLayout> </RelativeLayout>
3.drawable里的send_textview.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/zhuti"></solid> <corners android:radius="@dimen/_7dp"></corners> </shape>
4.drawable里的send_edittext.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="0dp"/> <solid android:color="#e9e9e9"/> </shape>
5.bean类
public class Chat { private String message; private int type; private String time; private String name; public Chat(String message, int type, String time) { this.message = message; this.type = type; this.time = time; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }
6.recyclerview适配器
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ public static final int TYPE_SEND=0; public static final int TYPE_RECEIVE=1; private Context context; private ArrayList<Chat> chatArrayList=new ArrayList<>(); public ChatAdapter(Context context, ArrayList<Chat> chatArrayList) { this.context = context; this.chatArrayList = chatArrayList; } @Override public int getItemViewType(int position) { if(chatArrayList.get(position).getType()==TYPE_SEND){ return TYPE_SEND; }else if(chatArrayList.get(position).getType()==TYPE_RECEIVE){ return TYPE_RECEIVE; }else{ return super.getItemViewType(position); } } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view; if(viewType==TYPE_SEND){ view= LayoutInflater.from(context).inflate(R.layout.item_send,parent,false); return new SendViewHolder(view); }else{ view= LayoutInflater.from(context).inflate(R.layout.item_receiver,parent,false); return new ReceiveViewHolder(view); } } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { if(holder instanceof SendViewHolder){ SendViewHolder viewHolder= (SendViewHolder) holder; viewHolder.textView_send.setText(chatArrayList.get(position).getMessage()); String end=chatArrayList.get(position).getTime(); if(position!=0){ String start=chatArrayList.get(position-1).getTime(); long time= 0; try { long ends=Long.valueOf(TimeStampUtils.Date2ms(end)); long starts=Long.valueOf(TimeStampUtils.Date2ms(start)); time = ends- starts; } catch (Exception e) { e.printStackTrace(); } long timestamp=Integer.valueOf(TimeStampUtils.stampToMinute(time)); if(timestamp>1){ viewHolder.time_send.setVisibility(View.VISIBLE); viewHolder.time_send.setText(TimeStampUtils.stampToDate(Long.parseLong(end))); }else{ viewHolder.time_send.setVisibility(View.GONE); } }else{ viewHolder.time_send.setVisibility(View.VISIBLE); viewHolder.time_send.setText(end); } }else if(holder instanceof ReceiveViewHolder){ ReceiveViewHolder viewHolder= (ReceiveViewHolder) holder; viewHolder.textView_receive.setText(chatArrayList.get(position).getMessage()); String end=chatArrayList.get(position).getTime(); if(position!=0){ String start=chatArrayList.get(position-1).getTime(); long time= 0; try { long ends=Long.valueOf(TimeStampUtils.Date2ms(end)); long starts=Long.valueOf(TimeStampUtils.Date2ms(start)); time = ends- starts; } catch (Exception e) { e.printStackTrace(); } long timestamp=Integer.valueOf(TimeStampUtils.stampToMinute(time)); if(timestamp>1){ viewHolder.time_receive.setVisibility(View.VISIBLE); viewHolder.time_receive.setText(end); }else{ viewHolder.time_receive.setVisibility(View.GONE); } }else{ viewHolder.time_receive.setVisibility(View.VISIBLE); viewHolder.time_receive.setText(end); } } } @Override public int getItemCount() { return chatArrayList.size(); } private class SendViewHolder extends RecyclerView.ViewHolder { private TextView textView_send, time_send; public SendViewHolder(View itemView) { super(itemView); textView_send = itemView.findViewById(R.id.right_msg); time_send = itemView.findViewById(R.id.time); } } private class ReceiveViewHolder extends RecyclerView.ViewHolder { private TextView textView_receive, time_receive; public ReceiveViewHolder(View itemView) { super(itemView); textView_receive = itemView.findViewById(R.id.left_msg); time_receive = itemView.findViewById(R.id.time); } } }
7.时间转换类
public class TimeStampUtils { /** * 获取当前时间戳 * @return */ public static String getTimeStamp() { long timeStamp = System.currentTimeMillis(); //String time = stampToMinute(timeStamp); return String.valueOf(timeStamp); } /** * 获取当前时间 * @return */ public static int[] getTime() { int[] time = new int[5]; //获取当前时间 Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int date = c.get(Calendar.DATE); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); time[0] = year; time[1] = month; time[2] = date; time[3] = hour; time[4] = minute; Log.d("xxxxx", year + "/" + (month + 1) + "/" + date + " " + hour + ":" + minute + ":" + second); return time; } /* * 将时间戳转换为时间 */ public static String stampToDate(long timeMillis) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(timeMillis); return simpleDateFormat.format(date); } /* * 将时间戳转换为时间 */ public static String stampToMinute(long timeMillis) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm"); Date date = new Date(timeMillis); return simpleDateFormat.format(date); } /* * 将时间转换为时间戳 */ public String dateToStamp(String time) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse(time); long ts = date.getTime(); return String.valueOf(ts); } public static String dateTosStamp(String time) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss"); Date date = simpleDateFormat.parse(time); long ts = date.getTime(); return String.valueOf(ts); } public static long Date2ms(String _data){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date = format.parse(_data); return date.getTime(); }catch(Exception e){ return 0; } } }
8.发送界面item_send.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="10dp"> <TextView android:id="@+id/time" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ccc" android:gravity="center" android:text="12:00" android:layout_marginBottom="10dp"/> <LinearLayout android:id="@+id/right_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:gravity="center" android:background="@drawable/sends"> <TextView android:id="@+id/right_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#fff" android:layout_marginBottom="3dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"/> </LinearLayout> </LinearLayout>
9.接收消息item_receive.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="10dp"> <TextView android:id="@+id/time" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="12:00" android:textColor="#ccc" android:layout_marginBottom="10dp"/> <LinearLayout android:id="@+id/left_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:background="@drawable/receives"> <TextView android:id="@+id/left_msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="7dp" android:textColor="#000" /> </LinearLayout> </LinearLayout>
10.activity类
private RecyclerView recyclerView_chat; private EditText editText_message; private TextView btn_send; private ChatAdapter chatAdapter; private ArrayList<Chat> chatArrayList = new ArrayList<>(); private int goodsId; private String message; private TextView back; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_module_difenna_customerservice); bindView(); recyclerView_chat = findViewById(R.id.recyclerView_chat); editText_message = findViewById(R.id.editText_message); btn_send = findViewById(R.id.btn_send); Intent intent = getIntent(); goodsId = intent.getIntExtra("goodsid", 0); getMessageList(UserUtils.getInstance().getUserInfo(getApplicationContext()).getTooken(), goodsId); chatArrayList.add(new Chat("你好啊。",ChatAdapter.TYPE_SEND,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("你叫什么名字啊?",ChatAdapter.TYPE_SEND,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("你好,我叫小猪",ChatAdapter.TYPE_RECEIVE,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("你是哪里人啊?",ChatAdapter.TYPE_SEND,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("我是湖南衡阳人",ChatAdapter.TYPE_RECEIVE,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("好巧啊,我也是衡阳的", ChatAdapter.TYPE_SEND, TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("咱们真有缘分诶",ChatAdapter.TYPE_SEND,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("我也觉得呢",ChatAdapter.TYPE_RECEIVE,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("我是湖南衡阳人",ChatAdapter.TYPE_RECEIVE,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("好巧啊,我也是衡阳的",ChatAdapter.TYPE_SEND,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("咱们真有缘分诶",ChatAdapter.TYPE_SEND,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("我也觉得呢",ChatAdapter.TYPE_RECEIVE,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("我是湖南衡阳人",ChatAdapter.TYPE_RECEIVE,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("好巧啊,我也是衡阳的",ChatAdapter.TYPE_SEND,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("咱们真有缘分诶",ChatAdapter.TYPE_SEND,TimeStampUtils.getTimeStamp())); chatArrayList.add(new Chat("我也觉得呢",ChatAdapter.TYPE_RECEIVE,TimeStampUtils.getTimeStamp())); chatAdapter = new ChatAdapter(CustomerserviceActivity.this, chatArrayList); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CustomerserviceActivity.this); recyclerView_chat.setLayoutManager(linearLayoutManager); recyclerView_chat.setAdapter(chatAdapter); btn_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { message = editText_message.getText().toString().trim(); if (!TextUtils.isEmpty(message)) { chatArrayList.add(new Chat(message, ChatAdapter.TYPE_SEND, TimeStampUtils.stampToDate(Long.parseLong(TimeStampUtils.getTimeStamp())))); chatAdapter.notifyDataSetChanged(); editText_message.setText(""); recyclerView_chat.scrollToPosition(chatArrayList.size() - 1); } } }); } // // public void bindView() { back = findViewById(R.id.back); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }