【问题标题】:Failed to show the grouping date view for the chat messages in the recyclerview android无法在 recyclerview android 中显示聊天消息的分组日期视图
【发布时间】:2021-04-12 12:32:10
【问题描述】:

如何在我的应用中进行分组并显示日期视图。我正在使用 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, parent, false);
                return new ReceiverMessageHolder(view);
            }
    
            if (viewType == DATE_VIEW_TYPE) {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.date_view, parent, 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);
}

【问题讨论】:

    标签: android android-recyclerview android-adapter sectionedrecyclerviewadapter


    【解决方案1】:

    由于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;
        }
    

    编辑:阅读下面的 cmets 后,您的问题要深得多,您正试图显示带有部分/组标题的单个 ArrayList 消息。

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

    How to implement RecyclerView with section header depending on category?

    【讨论】:

    • minemsg 是发件人 id 等于消息发件人 id。如何根据日期对消息进行分组并使用它显示日期视图
    • 为什么要先把日期记录详细说明一下?所有消息的日期都在那里。
    • 我不知道您的数据,您告诉我如何查看消息记录并确定它是 LEFT、RIGHT 还是 DATE 记录?如伪代码所示,布尔测试将通过或失败,即minemsg 为真或假,没有第三个条件
    • 它是聊天屏幕为发送者和接收者获取带有时间戳的消息。注意:按照您所说的设置日期后,我无法看到左侧和右侧的消息。 {“response”:{“senderid”:“myid”,“id”:“chat_id”,“msg”:“hai”,“timestamp”:“日期与时间”}{“senderid”:“otheruserid”,“ id": "chat_id", "msg": "hai', "timestamp":"date with time"}}}
    • 你是说我需要根据带有节标题的recyclerview更改整个视图吗?
    【解决方案2】:

    试试这个:

     @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;
            }
    

    【讨论】:

    • 按照你上面说的设置日期后,左右两边的消息都看不到了。
    • 那么,DATE_VIEW_TYPE 的所有观点都是?如果是 - 您需要对此进行调试,因为无法访问的代码(date_view 类型)不是您唯一的问题。
    • 是的,每条消息都有日期。
    • 我没有问你日期是否会在那里。我询问是否所有视图都是DATE_VIEW_TYPE 类型。基本上是问这个message.getmSentTime()的东西,它是否总是空的。
    • 如果 message.getmsenttime 总是有一个值(即)如果聊天中有可用的消息,它总是不为空
    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多