【问题标题】:How do I access the item in FirebaseRecycler's getItemViewType()?如何访问 FirebaseRecycler 的 getItemViewType() 中的项目?
【发布时间】:2018-06-29 12:29:56
【问题描述】:

我正在应用内构建聊天功能。用户发送的消息将在屏幕右侧,用户收到的消息将在屏幕左侧。所有聊天数据都存储在 pojo 中。在FirebaseRecycler#getItemViewType() 方法中,我想将当前项目/pojo 的UID 与mFirebaseUser.getUid() 进行比较,并相应地分配布局。

如何访问getItemViewType() 中的项目?

pojo 消息

public class message {

private String id;
private String text;
private String name;
private String photoUrl;
private String imageUrl;
private String uid;

public message() {
}

public message(String text, String name, String photoUrl, String imageUrl, String uid) {
    this.text = text;
    this.name = name;
    this.photoUrl = photoUrl;
    this.imageUrl = imageUrl;
    this.uid = uid;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public void setText(String text) {
    this.text = text;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhotoUrl() {
    return photoUrl;
}

public String getText() {
    return text;
}

public void setPhotoUrl(String photoUrl) {
    this.photoUrl = photoUrl;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getUid() {

    return uid;
}
}

FirebaseRecyler 方法。

private void getMessages() {

    final int VIEW_TYPE_MESSAGE_SENT = 1;
    final int VIEW_TYPE_MESSAGE_RECEIVED = 2;

    FirebaseRecyclerOptions<message> options =
            new FirebaseRecyclerOptions.Builder<message>()
                    .setQuery(messagesRef, parser)
                    .build();
    mFirebaseAdapter = new FirebaseRecyclerAdapter<message, RecyclerView.ViewHolder>(options) {

        @Override
        public int getItemViewType(int position) {
            if (mFirebaseUser.getUid() == ?) {
                // If the current user is the sender of the message
                // Based on the UID associated with the message and current UID
                return VIEW_TYPE_MESSAGE_SENT;

                } else {
                // If some other user sent the message
                return VIEW_TYPE_MESSAGE_RECEIVED;
            }
        }

        @Override
        @NonNull
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

            if(viewType == VIEW_TYPE_MESSAGE_SENT) {

                LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
                return new SentMessageViewHolder(inflater.inflate(R.layout.sent_item_message, viewGroup, false));

            } else if(viewType == VIEW_TYPE_MESSAGE_RECEIVED) {

                LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
                return new MessageViewHolder(inflater.inflate(R.layout.item_message, viewGroup, false));
            }

            return null;
        }

        @Override
        protected void onBindViewHolder(RecyclerView.ViewHolder viewHolder,
                                        int position,
                                        @NonNull message friendlyMessage) {
            mProgressBar.setVisibility(ProgressBar.INVISIBLE);
            switch (viewHolder.getItemViewType()) {
                case VIEW_TYPE_MESSAGE_RECEIVED:
                    ((MessageViewHolder) viewHolder).bind(friendlyMessage);
                    break;
                case VIEW_TYPE_MESSAGE_SENT:
                    ((SentMessageViewHolder) viewHolder).bind(friendlyMessage);
                    break;
            }

        }
    };

    mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

            ....
    });
}

【问题讨论】:

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


    【解决方案1】:

    让我们试试这个,

      if (mFirebaseUser.getUid() == options.get(position). getUid()) 
    

    【讨论】:

    • 这一直有效,直到片段处于活动状态,但在导航到其他活动并返回聊天片段后,所有消息都会显示在左侧。
    • 如果你浏览时间你的 mFirebaseUser.getUid() 日志......你会解决这个问题
    【解决方案2】:

    问题是如何访问getViewItemType() 中的item。为此,请使用 getItem(position) 方法。

        @Override
        public int getItemViewType(int position) {
              if (mFirebaseUser.getUid() == getItem(position).getUid()) {
                   // If the current user is the sender of the message
                   // Based on the UID associated with the message and current UID
                   return VIEW_TYPE_MESSAGE_SENT;
    
              } else {
                   // If some other user sent the message
                   return VIEW_TYPE_MESSAGE_RECEIVED;
              }
        }
    

    【讨论】:

    • 这一直有效,直到片段处于活动状态,但在导航到其他活动并返回聊天片段后,所有消息都会显示在左侧。
    • 然后尝试调试它,看看问题出在哪里。 mFirebaseUser.getUid() 错误或 getItem(position).getUid().
    • 问题在于 if 语句中的条件 TextUtils.equals() 解决了问题。
    【解决方案3】:

    正如其他答案所指出的那样,解决方案非常简单。 其他两个答案的唯一问题是条件检查 if 语句,它只在页面刷新之前显示发送的消息。解决方案是使用 TextUtils 类中的 equals() 方法。

    @Override
            public int getItemViewType(int position) {
                if (TextUtils.equals(mFirebaseUser.getUid(), getItem(position).getUid())) {
                    // If the current user is the sender of the message
                    return VIEW_TYPE_MESSAGE_SENT;
    
                    } else {
                    // If some other user sent the message
                    return VIEW_TYPE_MESSAGE_RECEIVED;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多