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

时间:2018-06-29 12:29:57

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

我正在构建应用程序中的聊天功能。用户发送的消息将显示在屏幕的右侧,而用户接收的消息将显示在屏幕的左侧。所有聊天数据都存储在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() {

            ....
    });
}

3 个答案:

答案 0 :(得分:2)

让我们尝试一下

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

答案 1 :(得分:1)

问题是如何在getViewItemType()中访问项目。为此,请使用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;
          }
    }

答案 2 :(得分:0)

如其他答案所指出的,解决方案非常简单。 其他两个答案的唯一问题是条件检查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;
            }
        }