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

How do I access the item in FirebaseRecycler's getItemViewType()?

我正在应用程序中构建聊天功能。用户发送的消息将显示在屏幕右侧,用户收到的消息将显示在屏幕左侧。所有聊天数据都存储在一个 pojo 中。在 FirebaseRecycler#getItemViewType() 方法中,我想将当前 item/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() {

            ....
    });
}

问题是如何访问 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;
          }
    }

试试这个,

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

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

中的 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;
            }
        }