使用Firebase时如何设计聊天说话布局有左侧和右侧?

时间:2017-03-09 04:35:42

标签: android firebase firebase-realtime-database

我使用Firebase实时数据库来完成聊天应用。

我想设计名称为Tom时,textView将显示在右侧,其他将显示在左侧。

这是我的布局:左右两个textView设计:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:background="@drawable/ic_send"
        android:id="@+id/button2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/button2"
        android:layout_toStartOf="@+id/button2"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

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

            <TextView
                android:id="@+id/textLeft"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="It's left >" />

            <TextView
                android:id="@+id/textRight"//It's for Tom
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="It's right >"/>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

这是我的聊天活动课程:

    public class Chat_Room extends AppCompatActivity {

        private Button button2;
        private TextView textLeft,textRight;
        private EditText editText2;

        private String room_name, user_name;
        private String myName;

        private DatabaseReference root;
        private DatabaseReference rootName;
        private String temp_key;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.chat_room);

            button2 = (Button) findViewById(R.id.button2);
            textLeft = (TextView) findViewById(R.id.textLeft);
            textRight = (TextView) findViewById(R.id.textRight);
            editText2 = (EditText) findViewById(R.id.editText2);

            user_name = getIntent().getExtras().getString("user_name").toString();
            room_name = getIntent().getExtras().getString("room_name").toString();

            setTitle("Room name:" + room_name);

            root = FirebaseDatabase.getInstance().getReference().child(room_name);//

            Log.d("Name is : ",rootName+"");

//send text msg
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Map<String, Object> map = new HashMap<>();
                    temp_key = root.push().getKey();
                    root.updateChildren(map);

                    DatabaseReference message_root = root.child(temp_key);
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("name", user_name);
                    map2.put("msg", editText2.getText().toString());

                    message_root.updateChildren(map2);
                }
            });

            root.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    myName= dataSnapshot.child("name").getValue(String.class);
                    Log.d("Name is : ",myName);

                        append_Right_chat_conversation(dataSnapshot);

                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                    //append_right_chat_conversation(dataSnapshot);
                }


                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

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

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }

        private String chat_msg, chat_user_name;


        private void append_Right_chat_conversation(DataSnapshot dataSnapshot) {
            Iterator i = dataSnapshot.getChildren().iterator();

            chat_msg=(String) ((DataSnapshot)(i.next())).getValue();
            chat_user_name=(String) ((DataSnapshot)(i.next())).getValue();

            textRight.append(chat_user_name+":"+chat_msg+" \n");

    //        textLeft.setGravity(Gravity.RIGHT);

        }
    }

这是我的数据库结构:

enter image description here

我尝试在onCreatView上使用此代码:

new Thread(new Runnable() {
            @Override
            public void run() {

                if (myName.equals("Tom")) {
                    textRight.setGravity(Gravity.RIGHT);
                } else {

                }

            }
        }).start();

但它不起作用。 :(

任何人都可以提供一些建议或样品吗?提前谢谢。

编辑 - 它显示如下:

![enter image description here] 2

0 个答案:

没有答案