无法在 recyclerview android 中显示聊天消息的分组日期视图 [英] Failed to show the grouping date view for the chat messages in the recyclerview android

查看:26
本文介绍了无法在 recyclerview android 中显示聊天消息的分组日期视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I make the grouping and show the date view in my app. I am using recyclerview and java to show this messages

结构:

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

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

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

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

The above is the message structure. I can able to show the rightside and leftside messages by the below code, But it failed to show the datetime view in my app.

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

Here how can I group and show the messages under a particular days message.

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

My Chat adapter code is here.

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

推荐答案

由于 minemsg 为 true 或 false,返回 DATE_VIEW_TYPE 的代码将永远无法到达.即,请遵循以下伪代码:

As minemsg is either true or false, the code returning DATE_VIEW_TYPE would never be reached. i.e., follow this pseudo-code:

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

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

Its not clear from your question what minemsg is, please try changing the order of evaluation as follows:

    @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 消息.

After reading your comments below, your problems are much deeper, you're trying to display a single ArrayList of messages with section/group headers.

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

This is beyond the scope of the question you asked, and appears to already have answers, e.g.:

如何使用节头实现 RecyclerView 取决于类别?

这篇关于无法在 recyclerview android 中显示聊天消息的分组日期视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆