Dynamic Section Header Of RecyclerView Using Current Date&Time

后端 未结 4 1925
轮回少年
轮回少年 2021-02-08 07:46

I\'ll use RecyclerView for a section header

I want to create section header when i insert each Date & Time data in SQLite Database

I followed

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-08 08:26

    Create two different ViewHolder's and chnage them by condition like chatting recycler view like this example . in your case :

    1. create variable String date = ""
    2. then check section header date is equal if not return header view and update the date to varibale (1st time not match date so show headerview)
    3. if date match return list_header_item_view
    
        public int getItemViewType(int position) {
                UserMessage message = (UserMessage) mMessageList.get(position);
    
                if (message.getSender().getUserId().equals(SendBird.getCurrentUser().getUserId())) {
                    // If the current user is the sender of the message
                    return VIEW_TYPE_MESSAGE_SENT;
                } else {
                    // If some other user sent the message
                    return VIEW_TYPE_MESSAGE_RECEIVED;
                }
            }
    
    
    
    
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view;
    
                if (viewType == VIEW_TYPE_MESSAGE_SENT) {
                    view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_sent, parent, false);
                    return new SentMessageHolder(view);
                } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
                    view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_received, parent, false);
                    return new ReceivedMessageHolder(view);
                }
    
                return null;
            }
    
    

提交回复
热议问题