无法在聊天室左右显示消息列表,总是在左侧(接收方)显示所有消息
Unable to show the message list in left and right side of the chat room, always showing all the message in left side(receiver side)
我正在使用 recyclerview 在我的应用程序中显示我的所有消息。但是总是在聊天室的左侧显示消息。
我怎样才能做到这一点?
这是我的代码:
这里我的适配器总是转到 else 部分并在屏幕左侧列出所有消息(即其他用户消息)
示例图片
适配器:
public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Messages> msgDtoList;
private final List<Messages> mData;
public static final int VIEW_TYPE_SENDER = 1;
public static final int VIEW_TYPE_RECEIVER = 2;
public ChatMessageAdapter(List<Messages> msgDtoList) {
this.msgDtoList = msgDtoList;
mData = msgDtoList;
}
public void setData(@Nullable Messages[] data) {
msgDtoList.clear();
for (Messages model : data) {
if (model instanceof Messages) {
mData.add(model);
continue;
}
}
@Override
public int getItemViewType(int position) {
Messages msgDto = this.msgDtoList.get(position);
if (msgDto.getmSender().getId() == Profile.getInstance().getId()) {
return VIEW_TYPE_SENDER;
}
else {
return VIEW_TYPE_RECEIVER;
}
}
@Override
public int getItemCount() {
return msgDtoList.size();
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
View v;
if(mMessages.getmSender().getId() == Profile.getInstance().getId()) {
sentViewHolder(holder, position);
} else if (msgDto.getmSender().getId() != Profile.getInstance().getId()) {
receivedViewHolder(holder, position);
}
}
@SuppressLint("ResourceAsColor")
private void sentViewHolder(RecyclerView.ViewHolder holder, int position) {
Messages msgDto = this.msgDtoList.get(position);
((SenderMessageHolder)holder).rightMsgTextView.setText(msgDto.getmMSg());
((SenderMessageHolder)holder).mDateTimestamp.setText(msgDto.getmTimestamp());
((SenderMessageHolder)holder).rightMsgTextView.setTextColor(R.color.yellow);
((SenderMessageHolder)holder).rightMsgTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
}
@SuppressLint("ResourceAsColor")
private void receivedViewHolder(RecyclerView.ViewHolder holder, int position) {
Messages msgDto = this.msgDtoList.get(position);
((ReceiverMessageHolder)holder).leftMsgTextView.setText(msgDto.getmMSg());
((ReceiverMessageHolder)holder).mDateTimestamp.setText(msgDto.getmTimestamp());
((ReceiverMessageHolder)holder).leftMsgTextView.setTextColor(R.color.red);
((ReceiverMessageHolder)holder).leftMsgTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Messages msgDto = this.msgDtoList.get(viewType);
if (msgDto.getmSender().getId() == Profile.getInstance().getId() ) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sender_message_layout, parent, false);
return new SenderMessageHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.receiver_message_layout, parent, false);
return new ReceiverMessageHolder(view);
}
}
}
}
在ChatFragment.java
@Override
public void onViewCreated(View v, @Nullable Bundle b) {
super.onViewCreated(v, b);
mChat = (TextView) v.findViewById(R.id.chat_messages);
mSendMsgInputBox = (EditText) v.findViewById(R.id.chat_input_msg);
mSendMsg = (ImageView) v.findViewById(R.id.chat_send_msg);
mChatMessageRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setSmoothScrollbarEnabled(true);
mChatMessageRecyclerView.setLayoutManager(linearLayoutManager);
final List<Messages> msgDtoList = new ArrayList<LastMessageContent>();
int newMsgPosition = msgDtoList.size() - 1;
mChatMessageRecyclerView.scrollToPosition(0);
mChatMessageRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
mChatMessageRecyclerView.setHasFixedSize(true);
mChatMessageRecyclerView.setAdapter(mAdapter);
ArrayList<Messages> messagesList = new ArrayList<>();
for (int i=0;i<10;i++) {
// messagesList.add(new LastMessageContent( mSendMsgInputBox.getText().toString(), Profile.getInstance().getId() == mChatMessage.getmSender().getId() ? ChatMessageAdapter.VIEW_TYPE_SENDER : ChatMessageAdapter.VIEW_TYPE_RECEIVER));
}
List<List<Messages>> itemList = new ArrayList<>();
mSendMsg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msgContent = mSendMsgInputBox.getText().toString();
if(!TextUtils.isEmpty(msgContent))
{
Messages msgDto = mChatMessage.getmessages();
msgDtoList.add(msgDto);
itemList.add(msgDtoList);
mAdapter.notifyDataSetChanged();
if (mSendMsgInputBox.toString() != "") {
LastMessageContent msgDto1 = new LastMessageContent(mChatMessage.getmLastMessageContent().getmMSg(), mChatMessage.getId());
System.out.println("msgDto1msgDto1 ==" + msgDto1);
msgDtoList.add(msgDto1);
}
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int messageCount = mAdapter.getItemCount();
int lastVisiblePosition =
linearLayoutManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the
// user is at the bottom of the list, scroll to the bottom
// of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
mChatMessageRecyclerView.scrollToPosition(positionStart);
}
}
});
}
}
您正在检查您的 viewType 两次。你不应该在 onCreateViewHolder 中再次检查它。
尝试这样的事情:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_SENDER) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sender_message_layout, parent, false);
return new SenderMessageHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.receiver_message_layout, parent, false);
return new ReceiverMessageHolder(view);
}
}
我正在使用 recyclerview 在我的应用程序中显示我的所有消息。但是总是在聊天室的左侧显示消息。
我怎样才能做到这一点?
这是我的代码:
这里我的适配器总是转到 else 部分并在屏幕左侧列出所有消息(即其他用户消息)
示例图片
适配器:
public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Messages> msgDtoList;
private final List<Messages> mData;
public static final int VIEW_TYPE_SENDER = 1;
public static final int VIEW_TYPE_RECEIVER = 2;
public ChatMessageAdapter(List<Messages> msgDtoList) {
this.msgDtoList = msgDtoList;
mData = msgDtoList;
}
public void setData(@Nullable Messages[] data) {
msgDtoList.clear();
for (Messages model : data) {
if (model instanceof Messages) {
mData.add(model);
continue;
}
}
@Override
public int getItemViewType(int position) {
Messages msgDto = this.msgDtoList.get(position);
if (msgDto.getmSender().getId() == Profile.getInstance().getId()) {
return VIEW_TYPE_SENDER;
}
else {
return VIEW_TYPE_RECEIVER;
}
}
@Override
public int getItemCount() {
return msgDtoList.size();
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
View v;
if(mMessages.getmSender().getId() == Profile.getInstance().getId()) {
sentViewHolder(holder, position);
} else if (msgDto.getmSender().getId() != Profile.getInstance().getId()) {
receivedViewHolder(holder, position);
}
}
@SuppressLint("ResourceAsColor")
private void sentViewHolder(RecyclerView.ViewHolder holder, int position) {
Messages msgDto = this.msgDtoList.get(position);
((SenderMessageHolder)holder).rightMsgTextView.setText(msgDto.getmMSg());
((SenderMessageHolder)holder).mDateTimestamp.setText(msgDto.getmTimestamp());
((SenderMessageHolder)holder).rightMsgTextView.setTextColor(R.color.yellow);
((SenderMessageHolder)holder).rightMsgTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
}
@SuppressLint("ResourceAsColor")
private void receivedViewHolder(RecyclerView.ViewHolder holder, int position) {
Messages msgDto = this.msgDtoList.get(position);
((ReceiverMessageHolder)holder).leftMsgTextView.setText(msgDto.getmMSg());
((ReceiverMessageHolder)holder).mDateTimestamp.setText(msgDto.getmTimestamp());
((ReceiverMessageHolder)holder).leftMsgTextView.setTextColor(R.color.red);
((ReceiverMessageHolder)holder).leftMsgTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Messages msgDto = this.msgDtoList.get(viewType);
if (msgDto.getmSender().getId() == Profile.getInstance().getId() ) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sender_message_layout, parent, false);
return new SenderMessageHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.receiver_message_layout, parent, false);
return new ReceiverMessageHolder(view);
}
}
}
}
在ChatFragment.java
@Override
public void onViewCreated(View v, @Nullable Bundle b) {
super.onViewCreated(v, b);
mChat = (TextView) v.findViewById(R.id.chat_messages);
mSendMsgInputBox = (EditText) v.findViewById(R.id.chat_input_msg);
mSendMsg = (ImageView) v.findViewById(R.id.chat_send_msg);
mChatMessageRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setSmoothScrollbarEnabled(true);
mChatMessageRecyclerView.setLayoutManager(linearLayoutManager);
final List<Messages> msgDtoList = new ArrayList<LastMessageContent>();
int newMsgPosition = msgDtoList.size() - 1;
mChatMessageRecyclerView.scrollToPosition(0);
mChatMessageRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
mChatMessageRecyclerView.setHasFixedSize(true);
mChatMessageRecyclerView.setAdapter(mAdapter);
ArrayList<Messages> messagesList = new ArrayList<>();
for (int i=0;i<10;i++) {
// messagesList.add(new LastMessageContent( mSendMsgInputBox.getText().toString(), Profile.getInstance().getId() == mChatMessage.getmSender().getId() ? ChatMessageAdapter.VIEW_TYPE_SENDER : ChatMessageAdapter.VIEW_TYPE_RECEIVER));
}
List<List<Messages>> itemList = new ArrayList<>();
mSendMsg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msgContent = mSendMsgInputBox.getText().toString();
if(!TextUtils.isEmpty(msgContent))
{
Messages msgDto = mChatMessage.getmessages();
msgDtoList.add(msgDto);
itemList.add(msgDtoList);
mAdapter.notifyDataSetChanged();
if (mSendMsgInputBox.toString() != "") {
LastMessageContent msgDto1 = new LastMessageContent(mChatMessage.getmLastMessageContent().getmMSg(), mChatMessage.getId());
System.out.println("msgDto1msgDto1 ==" + msgDto1);
msgDtoList.add(msgDto1);
}
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int messageCount = mAdapter.getItemCount();
int lastVisiblePosition =
linearLayoutManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the
// user is at the bottom of the list, scroll to the bottom
// of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
mChatMessageRecyclerView.scrollToPosition(positionStart);
}
}
});
}
}
您正在检查您的 viewType 两次。你不应该在 onCreateViewHolder 中再次检查它。
尝试这样的事情:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_SENDER) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sender_message_layout, parent, false);
return new SenderMessageHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.receiver_message_layout, parent, false);
return new ReceiverMessageHolder(view);
}
}