在用户聊天活动中组织消息

时间:2018-11-27 12:48:12

标签: java android firebase android-layout

我为我的应用程序创建了一个自定义视图持有人,以将发送的消息放置在右侧,将接收的消息放置在左侧,这是有效的。

在发送或接收消息时,它们将按预期放置,但是在关闭并重新打开聊天活动时,已发送和接收的消息都位于左侧,并使用相同的颜色。

即使关闭并重新打开应用程序,我如何使它保持原样?

下面是我的MessageAdapter.java

public class MessageAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private FirebaseAuth mAuth;


private Context mContext;
private List<Messages> mMessageList;

public MessageAdapter(List<Messages> mMessageList) {
    //mContext = context;

    this.mMessageList = mMessageList;

}


@Override
public int getItemCount() {
    return mMessageList.size();
}

// Determines the appropriate ViewType according to the sender of the message.
@Override
public int getItemViewType(int i) {

    mAuth = FirebaseAuth.getInstance();


    String current_user_id = mAuth.getCurrentUser().getUid();

    Messages c = mMessageList.get(i);

    String from_user = c.getFrom();
    String message_type = c.getType();


    if (from_user == (current_user_id)) {

        return VIEW_TYPE_MESSAGE_SENT;

    } else {

        return VIEW_TYPE_MESSAGE_RECEIVED;
    }
}

    // Inflates the appropriate layout according to the ViewType.
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent,int viewType){
        View view;

        if (viewType == VIEW_TYPE_MESSAGE_SENT) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_sent, parent, false);
            return new SentMessageHolder(view);
        } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_received, parent, false);
            return new ReceivedMessageHolder(view);
        }

        return null;
    }


    // Passes the message object to a ViewHolder so that the contents can be bound to UI.
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
        Messages c = mMessageList.get(i);

        switch (holder.getItemViewType()) {
            case VIEW_TYPE_MESSAGE_SENT:
                ((SentMessageHolder) holder).bind(c);
                break;
            case VIEW_TYPE_MESSAGE_RECEIVED:
                ((ReceivedMessageHolder) holder).bind(c);
        }
    }

    private class SentMessageHolder extends RecyclerView.ViewHolder {
        TextView messageText, timeText;

        SentMessageHolder(View itemView) {
            super(itemView);


            messageText = (TextView) itemView.findViewById(R.id.text_message_body);
            timeText = (TextView) itemView.findViewById(R.id.text_message_time);
        }

        void bind(Messages message) {
            messageText.setText(message.getMessage());

            // Format the stored timestamp into a readable String using method.
          //  timeText.setText(Utils.formatDateTime(message.getCreatedAt()));
        }
    }

    private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
        TextView messageText, timeText, nameText;
        ImageView profileImage;

        ReceivedMessageHolder(View itemView) {
            super(itemView);

            messageText = (TextView) itemView.findViewById(R.id.text_message_body);
            timeText = (TextView) itemView.findViewById(R.id.text_message_time);
        }

        void bind(Messages message) {
            messageText.setText(message.getMessage());

        }
    }
}

和我的ChatActivity.java

 LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View action_bar_view = inflater.inflate(R.layout.child_custom_layout,null);
    actionBar.setCustomView(action_bar_view);


    mTitleView = findViewById(R.id.custom_bar_title);
    mLastSeenView = findViewById(R.id.custom_bar_seen);
    mProfileImage = findViewById(R.id.custom_appbar_image);
    mChatAddBtn = findViewById(R.id.chat_add_btn);
    mChatSendBtn = findViewById(R.id.chat_send_btn);
    mChatMessageView = findViewById(R.id.chat_message_view);

    mChatAddBtn.setVisibility(View.INVISIBLE);
    mChatAddBtn.setEnabled(false);

    mTitleView.setText(userName);


    mAdapter = new MessageAdapter(messagesList);

    mMessagesList = findViewById(R.id.messages_list);
    mRefreshLayout = findViewById(R.id.message_swipe_layout);
    mLinearLayout = new LinearLayoutManager(this);

    mMessagesList.setHasFixedSize(true);
    mMessagesList.setLayoutManager(mLinearLayout);
    mMessagesList.setAdapter(mAdapter);

  //  loadMessages();


    mImageStorage = FirebaseStorage.getInstance().getReference();

    mRootRef.child("Chat").child(mCurrentUserId).child(mChatUser).child("seen").setValue(true);

    loadMessages();

1 个答案:

答案 0 :(得分:1)

这是代码中的问题

if (from_user == (current_user_id)) {

    return VIEW_TYPE_MESSAGE_SENT;

} else {

    return VIEW_TYPE_MESSAGE_RECEIVED;
}

您正在使用 == 检查等效性。这不是检查字符串等效性的正确方法。如果要检查等效性,则需要调用以下代码中提到的字符串的 equals()方法。

if (from_user.equals(current_user_id)) {

return VIEW_TYPE_MESSAGE_SENT;

} else {

return VIEW_TYPE_MESSAGE_RECEIVED;
}

尝试一下。希望对您有帮助。