How do I display the calendar date on the top of chat messages?

后端 未结 5 1118
离开以前
离开以前 2020-12-29 00:06

I am working on a chat application but I am struggling to figure out how to display the calendar date on the top of chat messages; for example, something like this:

5条回答
  •  情歌与酒
    2020-12-29 00:27

    Vilen answer is right. I am just improving perfomance. Instead of doing view gone and visible for every view, we can add a text view programmatically when condition true.

    
    
    
        
    
        
    
    
    

    ChatAdapter.java

    public class ChatEGRecyclerAdapter extends RecyclerView.Adapter {
    
        public static class TextViewHolder extends RecyclerView.ViewHolder {
            public TextView textView;
            public LinearLayout timeText;
            public TextViewHolder(View v) {
                super(v);
                timeText = (LinearLayout) v.findViewById(R.id.timeText);
                textView = (TextView) v.findViewById(R.id.textView);
            }
        }
    
        private final List messages;
    
    
        public ChatEGRecyclerAdapter(List messages) {
            this.messages = messages;
        }
    
    
        // Create new views (invoked by the layout manager)
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item, parent, false);
            return new TextViewHolder(v);
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
            final Message m = messages.get(position);
            final Context context = viewHolder.itemView.getContext();
    
            TextViewHolder holder = (TextViewHolder)viewHolder;
            holder.textView.setVisibility(View.VISIBLE);
            holder.textView.setText(m.getText());
    
            //Group by Date
            long previousTs = 0;
            if(position >= 1){
                Message previousMessage = messages.get(position-1);
                previousTs = Long.parseLong(previousMessage.getSentTime());
            }
            Calendar cal1 = Calendar.getInstance();
            Calendar cal2 = Calendar.getInstance();
            cal1.setTimeInMillis(Long.parseLong(messages.get(position).getSentTime())*1000);
            cal2.setTimeInMillis(previousTs*1000);
            boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                    cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
            if (!sameDay) {
                TextView dateView = new TextView(context);
                dateView.setText(messages.get(position).getSentTime());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER_HORIZONTAL;
                dateView.setLayoutParams(params);
                holder.timeText.addView(dateView);
            }
        }
    
    
        @Override
        public int getItemCount() {
            return messages.size();
        }
    
    }
    

    Also time formatting and checking for every message is expensive. To minimize this code execution actually we can compare the time stamp while sending the message. Get the time stamp of last message and compare with current message. If greater than 24 hrs then send to the server with isNewGroup:true. Then populating adapter can done without time comparison like below.

    @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
            final Message m = messages.get(position);
            final Context context = viewHolder.itemView.getContext();
    
            TextViewHolder holder = (TextViewHolder)viewHolder;
            holder.textView.setVisibility(View.VISIBLE);
            holder.textView.setText(m.getText());
    
            if (messages.get(position).getIsNewGroup()) {
                TextView dateView = new TextView(context);
                dateView.setText(messages.get(position).getSentTime());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER_HORIZONTAL;
                dateView.setLayoutParams(params);
                holder.timeText.addView(dateView);
            }
        }  
    

提交回复
热议问题