【问题标题】:RecyclerView for chat app has display issue using Friebase realtime database聊天应用程序的 RecyclerView 在使用 Firebase 实时数据库时出现显示问题
【发布时间】:2017-03-10 04:12:58
【问题描述】:

左边是玛丽,右边是比尔,说话人的 textView 会显示在右边,情况是玛丽说 1 和 2 ,看起来很好,就像照片一样:

下一步:右边的设备是比尔,他说 a 和 b,看起来不错,这正是我想要的。

当他们越来越多地输入单词时,我的问题是这样发生的:

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

所以我的问题是它的实时显示 recyclerView 有问题

我该如何解决这个问题,我尝试添加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;

关于 Firebase 数据库:

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);
        }
    }
}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

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

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

        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));
    
                }
    

    【讨论】:

    • 感谢您的回复,我更新了 rcyclerView 适配器的代码,根据 Visible,看起来我不使用它,我应该如何解决我的问题,提前致谢。
    • 再次查看我的答案
    • 感谢@Lalit Jadav,你解决了我的问题,真的很有帮助,上帝保佑你
    猜你喜欢
    • 2023-02-25
    • 2017-11-16
    • 2020-02-25
    • 2023-04-04
    • 2019-02-23
    • 1970-01-01
    • 2020-06-13
    • 2018-10-13
    • 1970-01-01
    相关资源
    最近更新 更多