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