【问题标题】:How to make firebase onDataChange() stop to call android?如何让firebase onDataChange() 停止调用android?
【发布时间】:2020-04-21 17:21:21
【问题描述】:

我正在我的项目中开发一个聊天模块。对于聊天模块,我使用的是火力基地。在我的项目中,有一个要求,例如当两个用户聊天时,如果用户 A 阻止用户 B,则用户 A 不应该从用户 B 收到任何类型的消息,就像用户 B 不应该从用户 A 收到任何消息一样,但他们仍然可以能够发送消息,但接收者不应该收到参考什么应用程序。我正在使用 fire base recyclerview 适配器来显示聊天消息。为了克服这个问题,我所做的是当用户被阻止时,当我在被阻止的情况下收到消息时,我只是将 recyclerview 参数设置为 0,因为我的 recyclerview 只是上下移动,这有点烦人。请在下面找到我的代码 sn-p 以及我的火基结构

case RECEIVER:
                        try {
                            listener = new ValueEventListener() {
                                @Override
                                public void onDataChange(final DataSnapshot dataSnapshot) {
                                    try {
                                        if (dataSnapshot != null) {
                                            Conversation conversationData = dataSnapshot.getValue(Conversation.class);
                                            if (!conversationData.isBlocked) {
                                                ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
                                                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                                                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                                                holder.itemView.setLayoutParams(layoutParams);
                                                HashMap<String, Object> hashMap = new HashMap<>();
                                                hashMap.put("isseen", true);
                                                dataSnapshot.getRef().updateChildren(hashMap);
                                                ((FirebaseConversationViewHolder) holder).messageText.setText(conversationData.getMessage());
                                                if (conversationData.getTimeStamp() != null) {
                                                    ((FirebaseConversationViewHolder) holder).timestamp.setText(TimeFormat.getTimeStamp(conversationData.getTimeStamp(), TimeFormatType.FORMAT_DESCRIPTION));
                                                }
                                                Picasso.with(getActivity())
                                                        .load(getResources().getString(R.string.image_loading_url) + friendProfilePath)
                                                        .placeholder(R.drawable.ic_profile).into(((FirebaseConversationViewHolder) holder).userProfile);
                                            } else {
                                                //firebaseRecyclerAdapter.stopListening();
                                                ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
                                                layoutParams.height = 0;
                                                layoutParams.width = 0;
                                                holder.itemView.setLayoutParams(layoutParams);
                                                //conversationDatabaseReference.child(firebaseRecyclerAdapter.getRef(position).getKey()).addListenerForSingleValueEvent(null);

                                                //holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
                                            }

                                        }

                                    } catch (Exception e) {
                                        Crashlytics.logException(e);
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {
                                    Log.d("cancel", "database");
                                }
                            };
                            conversationDatabaseReference.child(firebaseRecyclerAdapter.getRef(position).getKey()).addListenerForSingleValueEvent(listener);
                        } catch (Exception ex) {
                            Crashlytics.logException(ex);
                        }
                        break;

【问题讨论】:

    标签: android firebase firebase-realtime-database android-recyclerview chat


    【解决方案1】:

    请尝试布局 gone 而不是设置 height & width 0

    try {
                listener = new ValueEventListener() {
                    @Override
                    public void onDataChange(final DataSnapshot dataSnapshot) {
                        try {
                            if (dataSnapshot != null) {
                                Conversation conversationData = dataSnapshot.getValue(Conversation.class);
                                if (!conversationData.isBlocked) {
                                    //Your Code
                                } else {
                                    //Hide the layout
                                }
    
                            }
    
                        } catch (Exception e) {
                            Crashlytics.logException(e);
                        }
                    }
    
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.d("cancel", "database");
                    }
                };
                conversationDatabaseReference.child(firebaseRecyclerAdapter.getRef(position).getKey()).addListenerForSingleValueEvent(listener);
            } catch (Exception ex) {
                Crashlytics.logException(ex);
            }
    

    希望对你有帮助!

    谢谢。

    【讨论】:

    • 产生了什么问题?
    • 尝试布局其他部分
    • 如果我使用布局消失,那么它正在创建空白空间
    • 在这种情况下,你可以显示用户被阻止或你想要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多