gpt4 book ai didi

java - 左/右对齐消息

转载 作者:行者123 更新时间:2023-12-02 12:19:02 28 4
gpt4 key购买 nike

我使用 firebase API。发送和检索消息。但是,我在尝试为发送者/检索者设置布局时遇到麻烦,以便消息将左/右对齐。目前我只有发送者/检索者都使用的一种布局,但不确定如何设置不同的布局。

public class MessageAdapter extends RecyclerView.Adapter<MessageViewHolder> {

private List<Message> messagesList;

private DatabaseReference databaseReference;
private DatabaseReference userDatabaseRef;
private FirebaseAuth firebaseAuth;
private Context context;
private String imageUrl;


public MessageAdapter(List<Message> messagesList) {
this.messagesList = messagesList;
}

@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Message c = messagesList.get(viewType);
String sender = c.getFrom();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(sender);

View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.messages_layout, parent, false);
return new MessageViewHolder(v);


}



@Override
public void onBindViewHolder(final MessageViewHolder viewHolder, final int index) {


final Message c = messagesList.get(index);

final String sender = c.getFrom();

databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(sender);

databaseReference.addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot dataSnapshot) {

imageUrl = dataSnapshot.child("image").getValue(String.class);

viewHolder.setUserimage(context,imageUrl);


String name = dataSnapshot.child("name").getValue().toString();
viewHolder.displayName.setText(name);

}
@Override
public void onCancelled(DatabaseError databaseError) {
}

});

viewHolder.messageText.setText(c.getMessage());
viewHolder.time.setText(EpochtimeToDateAndTimeString(c.getTime()));
}

@Override
public int getItemCount() {
return messagesList.size();
}


public String EpochtimeToDateAndTimeString(long time) {
Date date = new Date(time);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
return formatted;
}



}

MessageViewHolder

public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView messageText;
public TextView displayName;
public TextView time;
public ImageView showImage;

public MessageViewHolder(View view) {
super(view);
messageText = (TextView) view.findViewById(R.id.messagetext);
displayName = (TextView) view.findViewById(R.id.displayname);
time = (TextView) view.findViewById(R.id.timestamp);
showImage = (ImageView) view.findViewById(R.id.imageview_post_userimage3);
}

最佳答案

您应该为发送者和接收者的消息使用不同的 View 持有者。

创建两个布局:R.layout.sender_message_layoutR.layout.receiver_message_layout

创建两个 View 持有者:SenderMessageHolderReceiverMessageHolder

定义 View 类型整数常量:

public static final int VIEW_TYPE_SENDER = 1;
public static final int VIEW_TYPE_RECEIVER = 2;

实现getItemViewType(int position)在您的适配器上:

if(messageList.get(position).getFrom().equals(...)) {
return VIEW_TYPE_SENDER;
} else {
return VIEW_TYPE_RECEIVER;
}

onCreateViewHolder()函数中检查 View 类型并为不同的发送者创建不同的持有者:

View v;

if(viewType == VIEW_TYPE_SENDER) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sender_message_layout, parent, false);
return new SenderMessageHolder(v);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.receiver_message_layout, parent, false);
return new ReceiverMessageHolder(v);
}

在 onBindViewHolder() 中检查 Holder 实例以相应地将值绑定(bind)到 View :

if(holder instanceof SenderMessageHolder) {
((SenderMessageHolder) holder).textView.setText(...);
...
} else {
((ReceiverMessageHolder) holder).textView.setText(...);
...
}

希望对你有帮助!

关于java - 左/右对齐消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45926576/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com