无法在 recyclerview android 中显示聊天消息的分组日期视图

问题描述

如何进行分组并在我的应用中显示日期视图。我正在使用 recyclerview 和 java 来显示此消息

结构:

   --------- **12-04-2021** ----------

--Leftsidemessage--
          ----rightsidemessgae----
--Leftsidemessage--
          ----rightsidemessgae----
--Leftsidemessage--
          ----rightsidemessgae----

    --------- **13-04-2021** ----------
--Leftsidemessage--
          ----rightsidemessgae----
--Leftsidemessage--
          ----rightsidemessgae----
--Leftsidemessage--
          ----rightsidemessgae----

以上是消息结构。我可以通过下面的代码显示右侧和左侧消息,但无法在我的应用中显示日期时间视图。

这里我如何分组和显示特定日期消息下的消息。

我的聊天适配器代码在这里

 @Override
        public int getItemViewType(int position) {
            Message message = (Message) msgDtoList.get(position);
            if (minemsg) {
                // If the current user is the sender of the message
                return VIEW_TYPE_MESSAGE_SENT;
            }
            if (!minemsg) {
                // If some other user sent the message
                return VIEW_TYPE_MESSAGE_RECEIVED;
            }
    
            if (message.getmSentTime() != "") { // here the message senttime is always print inside this condition. But Failed to show the view in the app.
                return DATE_VIEW_TYPE;
            }
    
            return 0;
        }
     @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
            View view;
    
            if (viewType == VIEW_TYPE_MESSAGE_SENT) {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.sender_message_layout,parent,false);
                return new SenderMessageHolder(view);
    
            }
            if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.receiver_message_layout,false);
                return new ReceiverMessageHolder(view);
            }
    
            if (viewType == DATE_VIEW_TYPE) {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.date_view,false);
                return new DateViewHolder(view);
            }
    
            return null;
        }

 @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,int position) {
        Message message = (Message) msgDtoList.get(position);

        switch (holder.getItemViewType()) {
            case VIEW_TYPE_MESSAGE_SENT:
                populateSentViewHolder(holder,position);
                break;
            case VIEW_TYPE_MESSAGE_RECEIVED:
                populateReceivedViewHolder(holder,position);
                break;

            case DATE_VIEW_TYPE:
                populateDateViewHolder(holder,position);
                break;
        }
    }

我的聊天片段:

  @Override
    public void onViewCreated(View v,@Nullable Bundle b) {
        super.onViewCreated(v,b);
        linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(getActivity());
        linearlayoutmanager.setorientation(linearlayoutmanager.VERTICAL);
        linearlayoutmanager.setStackFromEnd(true);
        linearlayoutmanager.setSmoothScrollbarEnabled(true);
        linearlayoutmanager.setReverseLayout(false);
        mRecyclerView.setLayoutManager(linearlayoutmanager);

        int newMsgPosition = mAdapter.getItemCount();
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);
}

解决方法

由于 minemsg 为真或假,返回 DATE_VIEW_TYPE 的代码永远不会到达。即,请遵循以下伪代码:

boolean minemsg = true;
if (minemsg)
{
  return;
}
if (!minemsg)
{
  return;
}
// anything after this would never be reached!,as minemsg is either true or false.

从您的问题中不清楚 minemsg 是什么,请尝试按如下方式更改评估顺序:

    @Override
    public int getItemViewType(int position) {
        Message message = (Message) msgDtoList.get(position);
        
        // check for date record first
        if (message.getmSentTime() != "") { // here the message senttime is always print inside this condition. But failed to show the view in the app.
            return DATE_VIEW_TYPE;
        }

        if (minemsg) {
            // If the current user is the sender of the message
            return VIEW_TYPE_MESSAGE_SENT;
        }
        if (!minemsg) {
            // If some other user sent the message
            return VIEW_TYPE_MESSAGE_RECEIVED;
        }

        
        return 0;
    }

编辑:在阅读下面您的评论后,您的问题更深了,您正在尝试显示带有部分/组标题的单个 ArrayList 消息。

这超出了您提出的问题的范围,并且似乎已经有了答案,例如:

How to implement RecyclerView with section header depending on category?

,

试试这个:

 @Override
        public int getItemViewType(int position) {
            Message message = (Message) msgDtoList.get(position);
    
            // Since message can either be "mine" or "not mine" lets check the sent time first,and return the DATE_VIEW_TYPE to avoid dead code
            if (message.getmSentTime() != "") { // here the message senttime is always print inside this condition. But failed to show the view in the app.
                return DATE_VIEW_TYPE;
            }

            if (minemsg) {
                // If the current user is the sender of the message
                return VIEW_TYPE_MESSAGE_SENT;
            }
            if (!minemsg) {
                // If some other user sent the message
                return VIEW_TYPE_MESSAGE_RECEIVED;
            }
            // Everything below is "dead,unreachable code" due to booleans being either true or false
    
            return 0;
        }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...