RecyclerView for chat app使用Friebase实时数据库显示问题

时间:2017-03-10 04:12:59

标签: android firebase firebase-realtime-database

左边是Mary,右边是Bill,扬声器的textView将显示在右侧,情况是Mary说1和2,它看起来很好,就像照片一样: enter image description here

下一步:右侧设备是比尔谁说a和b,它看起来很好,这正是我想要的。 enter image description here

当他们越来越多地输入单词时,我的问题是这样的: enter image description here

但是当我离开ChatGroup1并再次输入时,它看起来很好 enter image description here

所以我的问题是它在显示recyclelerView

上的实时性有问题

如何解决此问题,我尝试添加listData.clear();,它无效

任何帮助将不胜感激,提前谢谢。

这是我的聊天频道布局:

<?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="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2">
    </android.support.v7.widget.RecyclerView>

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


    <EditText
        android:gravity="center"
        android:hint="Type your message..."
        android:background="@drawable/edit_corner"
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/button2"
        android:layout_toStartOf="@+id/button2" />


</RelativeLayout>

这是我的Firebase和recyclerView的聊天课程:

//global variable
private RecyclerView recyclerChat;
private ArrayList<ChatItem> listData = new ArrayList<>();
private RecyclerChatItemAdapter adapter;
private DatabaseReference root;

关于FirebaseDatabase:

root = FirebaseDatabase.getInstance().getReference().child(room_name);
//send message to FirebaseDatabase
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);
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken() , InputMethodManager.HIDE_NOT_ALWAYS);//just hide the keyboard when send message
                editText2.setText("");
            }
        });

我认为这可能是我的问题就在这里,关于我的recyclerView:

root.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                ChatItem chatItem = dataSnapshot.getValue(ChatItem.class);//put the data to my javabean from Firebase

                Log.d("Contacts:   ", chatItem.toString());

                listData.add(chatItem);
                recyclerChat.scrollToPosition(adapter.getItemCount() - 1);//just let the latest data below
                adapter.notifyDataSetChanged();

            }

            @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) {

            }
        });


        LinearLayoutManager manager = new LinearLayoutManager(this);
        //        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerChat.setLayoutManager(manager);

        adapter = new RecyclerChatItemAdapter(this, listData);
        recyclerChat.setAdapter(adapter);
        adapter.notifyDataSetChanged();//when i leave the chat group and enter again , it looks fine because this.

这是我的recyclerView适配器:

public class RecyclerChatItemAdapter extends RecyclerView.Adapter<RecyclerChatItemAdapter.MyViewHolder>{

    private Context context;
    private List<ChatItem> mDatas;

    public RecyclerChatItemAdapter(Context context, List<ChatItem> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(parent.getContext(),R.layout.for_chat_item_layout,null);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        String myName=mDatas.get(position).getName();
        Contacts contacts=Contacts.getContacts();
        String createName=contacts.getName();
        if (myName.equals(createName)){
            holder.textRight.setText(mDatas.get(position).getMsg()+"("+mDatas.get(position).getName()+")");
            holder.textRight.setTextColor(Color.BLUE);
            holder.textRight.setBackground(context.getResources().getDrawable(R.drawable.chat_green));

        }else {
            holder.textLeft.setText(mDatas.get(position).getMsg()+"("+mDatas.get(position).getName()+")");
            holder.textLeft.setTextColor(Color.RED);
            holder.textLeft.setBackground(context.getResources().getDrawable(R.drawable.chat_blue));

        }
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView textLeft,textRight;

        public MyViewHolder(View itemView) {
            super(itemView);
            textLeft=(TextView)itemView.findViewById(R.id.textLeft);
            textRight=(TextView)itemView.findViewById(R.id.textRight);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您没有显示适配器布局和适配器代码......它可能是视图的可见性问题。

您可能会为视图设置可见性..因此,请确保您已编写类似的内容..

    if (myName.equals(createName)){
                holder.textRight.setVisibility(VISIBLE);
                holder.textLeft.setVisibility(GONE);
                holder.textRight.setText(mDatas.get(position).getMsg()+"("+mDatas.get(position).getName()+")");
                holder.textRight.setTextColor(Color.BLUE);
                holder.textRight.setBackground(context.getResources().getDrawable(R.drawable.chat_green));

            }else {
                holder.textRight.setVisibility(GONE);
                holder.textLeft.setVisibility(VISIBLE);
                holder.textLeft.setText(mDatas.get(position).getMsg()+"("+mDatas.get(position).getName()+")");
                holder.textLeft.setTextColor(Color.RED);
                holder.textLeft.setBackground(context.getResources().getDrawable(R.drawable.chat_blue));

            }