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

时间:2017-01-08 05:53:24

标签: java android xml

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

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>

请帮忙。 提前谢谢!

2 个答案:

答案 0 :(得分:1)

将此用作XML文件

#!/usr/bin/env python

# Waits for the parent process to terminate, then executes specified commands.

import os
import signal
import sys
import time

if len(sys.argv) < 3:
    raise Exception('usage: restart.py <pid> <path> [optional command]')

# signal.signal(signal.SIGHUP, signal.SIG_IGN)

pid = int(sys.argv[1])
while os.getppid() == pid:
    time.sleep(0.5)

if len(sys.argv) > 3:

to_launch = ['/usr/bin/open', sys.argv[2]] if sys.platform == 'darwin' else [sy$
os.execv(to_launch[0], to_launch)

如果使用Layoutparams.you的当前用户可以使用View.getLayoutParams从代码访问任何LayoutParams以对齐当前用户的父。

对齐父权。

<?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>

答案 1 :(得分:1)

您必须处理视图的gravity属性。这可以通过编程方式完成:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Message m = Message.get(position);
    holder.message_text.setText(m.getText());

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    params.weight = 1.0f;        
    if (holder.user.getUsername().equals(m.getUsername())) {
        params.gravity = Gravity.LEFT;
    } else {
        params.gravity = Gravity.RIGHT;
    }
    holder.message_text.setLayoutParams(params);        

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