gpt4 book ai didi

java - 无法根据 RecyclerView 中的发件人对齐聊天消息

转载 作者:搜寻专家 更新时间:2023-11-01 09:38:11 25 4
gpt4 key购买 nike

我正在使用 RecyclerView。我正在向适配器传递一个消息对象列表。我想根据发件人组织或对齐聊天消息。如果发件人是当前用户本人,则消息将靠右对齐,否则靠左对齐。尝试了很多方法。仍然无法得到解决方案!

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder>{
private List<Message> Message;
private Context context;

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView message_text;
public User user;
public ViewGroup parent;

public ViewHolder(View itemView) {
super(itemView);
message_text = (TextView) itemView.findViewById(R.id.message_text);

user = Methods.getUserInstance(message_text.getContext());
}
}
public MessageAdapter(Context context, List<Message> message) {
this.context = context;
this.Message = message;
}

public void add(int position, Message message) {
Message.add(position, message);
}

@Override
public MessageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_message, parent, false);
ViewHolder vh = new ViewHolder(v);
vh.parent = parent;
return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Message m = Message.get(position);
holder.message_text.setText(m.getText());
if (holder.user.getUsername().equals(m.getUsername())) {
//here goes the manipulation!
}

Toast.makeText(holder.message_text.getContext(), m.getUsername(), Toast.LENGTH_SHORT).show();
}

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

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1"
android:paddingRight="10dp"
android:paddingLeft="10dp">

<LinearLayout
android:orientation="horizontal"
android:layout_width="289dp"
android:layout_height="wrap_content"
android:id="@+id/linearLayout">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:id="@+id/message_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/reciever_rounded_message"
android:padding="16dp"
android:text="chakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakkachakka" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

请帮忙。提前致谢!

最佳答案

将其用作 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingLeft="10dp">

<TextView
android:id="@+id/reply_text"
android:layout_width="289dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="some text here!" />

</RelativeLayout>

如果当前用户使用 Layoutparams,则父级右对齐。如果当前用户,您可以使用 View.getLayoutParams 从代码访问任何 LayoutParams 以对齐父级。

if (holder.user.getUsername().equals(m.getUsername())) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)message_text.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
message_text.setLayoutParams(params);
}

关于java - 无法根据 RecyclerView 中的发件人对齐聊天消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41529923/

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