1

如何进行分组并在我的应用程序中显示日期视图。我正在使用 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);
}
4

2 回答 2

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

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

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

如何根据类别实现带有节标题的 RecyclerView?

于 2021-04-16T11:07:11.373 回答
0

尝试这个:

 @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;
        }
于 2021-04-16T11:11:43.060 回答