Ошибка выравнивания чата в Firebase Android - PullRequest
0 голосов
/ 25 мая 2018

Я относительно новичок в Android Dev't.Я создал довольно хорошую систему чата, но не могу отобразить сообщение отправителя слева, а получателя - справа.Пытался использовать метод гравитации, но не получилось.Кто-нибудь сталкивался с подобной ошибкой?Пожалуйста помоги.Ниже приведен мой код.

Активность чата

package com.dreamlazerstudios.gtuconline;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ServerValue;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChatActivity extends AppCompatActivity {

private DatabaseReference rootRef, messagesRef;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private String userID;
private String mChatUser;

private ImageButton chat_add_btn, chat_send_btn;
private EditText enter_message;

List<DataSnapshot> listData;
RecyclerView recyclerView;
ChatActivity.MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    recyclerView = findViewById(R.id.messages_list);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);

    messagesRef = FirebaseDatabase.getInstance().getReference();

    listData = new ArrayList<>();
    adapter = new ChatActivity.MyAdapter(listData);
    adapter.setHasStableIds(true);

    chat_add_btn = findViewById(R.id.chat_add_btn);
    chat_send_btn = findViewById(R.id.chat_send_btn);
    enter_message = findViewById(R.id.chat_message_view);

    mChatUser = getIntent().getStringExtra("user_id");
    String username = getIntent().getStringExtra("user_name");
    setTitle(username);

    rootRef = FirebaseDatabase.getInstance().getReference();
    mAuth = FirebaseAuth.getInstance();
    final FirebaseUser user = mAuth.getCurrentUser();
    userID = user.getUid();

    loadMessages();

    rootRef.child("Chat").child(userID).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if (!dataSnapshot.hasChild(mChatUser)) {
                Map chatAddMap = new HashMap();
                chatAddMap.put("seen", false);
                chatAddMap.put("timestamp", ServerValue.TIMESTAMP);

                Map chatUserMap = new HashMap();
                chatUserMap.put("Chat/" + userID + "/" + mChatUser, chatAddMap);
                chatUserMap.put("Chat/" + mChatUser + "/" + userID, chatAddMap);

                rootRef.updateChildren(chatUserMap, new DatabaseReference.CompletionListener() {
                    @Override
                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                        if (databaseError != null) {

                            Log.d("CHAT_LOG", databaseError.getMessage().toString());

                        }
                    }
                });
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    chat_send_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendMessage();
        }
    });

}

private void loadMessages() {
    messagesRef = FirebaseDatabase.getInstance().getReference().child("messages").child(userID).child(mChatUser);

    messagesRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Messages messages = dataSnapshot.getValue(Messages.class);

            listData.add(dataSnapshot);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private void sendMessage() {
    String message = enter_message.getText().toString();
    if (!TextUtils.isEmpty(message)) {

        String current_user_ref = "messages/" + userID + "/" + mChatUser;
        String chat_user_ref = "messages/" + mChatUser + "/" + userID;

        DatabaseReference user_message_push = rootRef.child("messages").child(userID).child(mChatUser).push();

        String push_id = user_message_push.getKey();

        Map messageMap = new HashMap();
        messageMap.put("message", message);
        messageMap.put("seen", false);
        messageMap.put("type", "text");
        messageMap.put("time", ServerValue.TIMESTAMP);
        messageMap.put("from", userID);

        Map messageUserMap = new HashMap();
        messageUserMap.put(current_user_ref + "/" + push_id, messageMap);
        messageUserMap.put(chat_user_ref + "/" + push_id, messageMap);

        enter_message.setText("");

        rootRef.updateChildren(messageUserMap, new DatabaseReference.CompletionListener() {
            @Override
            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                if (databaseError != null) {
                    Log.d("CHAT_LOG", databaseError.getMessage());
                }

            }
        });
    }
}

public class MyAdapter extends RecyclerView.Adapter<ChatActivity.MyAdapter.ViewHolder> {

    List<DataSnapshot> list;

    public MyAdapter(List<DataSnapshot> List) {
        this.list = List;
    }

    @Override
    public void onBindViewHolder(@NonNull final ChatActivity.MyAdapter.ViewHolder holder, final int position) {

        final DataSnapshot studentSnapshot = list.get(position);

        final Messages students = studentSnapshot.getValue(Messages.class);

        assert students != null;
        holder.messageText.setText(students.getMessage());

        String current_user_id = mAuth.getCurrentUser().getUid();
        String from_user = students.getFrom();

        if (from_user.equals(current_user_id)) {

            holder.messageText.setBackgroundResource(R.drawable.message_text_background1);
            holder.messageText.setTextColor(Color.BLACK);
            holder.messageText.setGravity(Gravity.END | Gravity.RIGHT);

        } else {

            holder.messageText.setBackgroundResource(R.drawable.message_text_background);
            holder.messageText.setTextColor(Color.WHITE);
            holder.messageText.setGravity(Gravity.START | Gravity.LEFT);

        }


    }

    @NonNull
    @Override
    public ChatActivity.MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_single_layout, parent, false);

        return new ChatActivity.MyAdapter.ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView messageText;

        public ViewHolder(View itemView) {
            super(itemView);

            messageText = itemView.findViewById(R.id.user_single_message);

        }

    }

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


}

Мой макет xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightgrey"
tools:context=".ChatActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/messages_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/text_layout">

</android.support.v7.widget.RecyclerView>

<LinearLayout
    android:id="@+id/text_layout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:weightSum="100">


    <ImageButton
        android:id="@+id/chat_add_btn"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:background="@color/white"
        android:contentDescription="@string/image"
        android:src="@drawable/ic_add_black_24dp" />

    <EditText
        android:id="@+id/chat_message_view"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="60"
        android:background="@color/white"
        android:ems="10"
        android:hint="Send a message"
        android:inputType="none"
        android:paddingEnd="3dp"
        android:paddingStart="3dp"
        android:paddingTop="5dp"
        android:textSize="17sp" />

    <ImageButton
        android:id="@+id/chat_send_btn"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:background="@color/white"
        android:contentDescription="@string/send"
        android:src="@drawable/ic_send_black_24dp" />


</LinearLayout>

</RelativeLayout>

Макетдля переработчика

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

    <TextView
        android:layout_margin="15dp"
        android:id="@+id/user_single_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:padding="13dp"
        android:textColor="@color/white"
        android:background="@drawable/message_text_background"
        android:layout_marginStart="10dp"
        android:text="Message Text"
        android:textSize="15sp" />

</LinearLayout>

1 Ответ

0 голосов
/ 25 мая 2018

У меня была та же проблема, я просто проверяю, совпадает ли текущий идентификатор пользователя с идентификатором отправителя сообщения, и я решил изменить адаптер следующим образом:

@Override
public void onBindViewHolder(MessageHolder holder, final int position) {
     if (conversationList != null){
         if (conversationList.get(position).getMessageSenderId().equals(User.getId())) {
            holder.chatBubbleLeft.setVisibility(View.INVISIBLE);
            holder.chatBubbleRight.setVisibility(View.VISIBLE);
            holder.chatBubbleRight.setText(conversationList.get(position).getMessageText());
        } else {
            holder.chatBubbleRight.setVisibility(View.INVISIBLE);
            holder.chatBubbleLeft.setVisibility(View.VISIBLE);
            holder.chatBubbleLeft.setText(conversationList.get(position).getMessageText());
        }
    }
}

с этим макетом:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
        <TextView
            android:id="@+id/text_view_chat_bubble_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="25dp"
            android:background="@drawable/bubble_left"
            android:gravity="start"
            android:paddingBottom="5dp"
            android:paddingEnd="10dp"
            android:paddingStart="10dp"
            android:paddingTop="5dp"
            android:textAlignment="textStart"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:visibility="gone" />
        <TextView
            android:id="@+id/text_view_chat_bubble_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="5dp"
            android:layout_marginStart="25dp"
            android:background="@drawable/bubble_right"
            android:gravity="start"
            android:paddingBottom="5dp"
            android:paddingEnd="10dp"
            android:paddingStart="10dp"
            android:paddingTop="5dp"
            android:textAlignment="textStart"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:visibility="gone" />
</RelativeLayout>
...