在“onBindViewHolder”方法中,可以通过检查每个消息是否发送来处理视图的显示。例如,可以添加以下代码:
if(mDataList.get(position).isSent()){ holder.sentLayout.setVisibility(View.VISIBLE); holder.receivedLayout.setVisibility(View.GONE); holder.msgSent.setText(mDataList.get(position).getMessage()); }else{ holder.sentLayout.setVisibility(View.GONE); holder.receivedLayout.setVisibility(View.VISIBLE); holder.msgReceived.setText(mDataList.get(position).getMessage()); }
其中,“isSent()”方法用于检查消息是否已发送。短信数据可以使用以下代码创建:
public class MessageData { private String message; private boolean sent;
public MessageData(String message, boolean sent) {
this.message = message;
this.sent = sent;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isSent() {
return sent;
}
public void setSent(boolean sent) {
this.sent = sent;
}
}
这将在Recycler View中显示发送和接收的短信消息。