更改recyclerview(firebase)中已接收和已发送消息的背景

时间:2018-09-22 23:25:49

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

iam试图将imageview手风琴的背景图像更改为发送消息的人 如果是我,那么背景就是我的照片 如果是其他背景将变为匿名 问题是 我的代码正常工作,但是 有时它像 我的意思是说发送消息的背景是绿色,接收到的是黄色 有时发送的是黄色,接收的是绿色 当我刷新布局时 它完美地工作 这是我的代码

首先是我的回收站视图

private void intitializingRecyclerView()
{

    mMessagesList.setLayoutManager(new LinearLayoutManager(this));
    Query mQuery = mMyMessagesRef;

    FirebaseRecyclerOptions mOptions = new FirebaseRecyclerOptions.Builder<Message>()
            .setQuery(mQuery,Message.class).build();


    mAdapter = new FirebaseRecyclerAdapter<Message, MessagesHolder>(mOptions) {
        @Override
        protected void onBindViewHolder(@NonNull MessagesHolder holder, int position, @NonNull Message model) {
            if (model.getFrom().equals(myId))
            {
                holder.fillSentMessage(Image,model.getMessages());
            } else if (!model.getFrom().equals(myId))
            {
                holder.fillReceivedMessage("",model.getMessages());
            }
            holder.showElements();
        }

        @NonNull
        @Override
        public MessagesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

             View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_sent_message,parent,false);
            return new MessagesHolder(view,parent.getContext());
        }
    };
    mMessagesList.setAdapter(mAdapter);

}

然后我在sendButton中写的就是这个

mMessage.setText("");
mMessagesList.scrollToPosition(count - 1);
mAdapter.notifyDataSetChanged();

和我的HolderClass

public class MessagesHolder extends RecyclerView.ViewHolder {

    private CircleImageView mImage;
    private TextView mMessage;
    private ConstraintLayout mLayout;

    Context context;

    public MessagesHolder(@NonNull View itemView, Context context) {
        super(itemView);
        this.context = context;

        mImage = itemView.findViewById(R.id.iv_sent_message);
        mMessage = itemView.findViewById(R.id.tv_sent_message);
        mLayout = itemView.findViewById(R.id.cl_message_layout);

    }


    public void fillSentMessage(final String Image, String Message) {
        Picasso.get().load(Image).networkPolicy(NetworkPolicy.OFFLINE).into(mImage, new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError(Exception e) {
                Picasso.get().load(Image).into(mImage);
            }
        });
        mMessage.setText(Message);
    }

    public void fillReceivedMessage(final String Image, String Message) {

        mLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
        if (!Image.isEmpty()) {

            Picasso.get().load(Image).networkPolicy(NetworkPolicy.OFFLINE).into(mImage, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError(Exception e) {
                    Picasso.get().load(Image).into(mImage);
                }
            });
        } else
        {
            Picasso.get().load(R.drawable.anynomous).into(mImage);
        }
        mMessage.setTextColor(context.getResources().getColor(R.color.white));
        mMessage.setText(Message);
        mMessage.setBackgroundResource(R.drawable.recieved_message);

    }

    public void showElements()
    {
        mMessage.setVisibility(View.VISIBLE);
        mImage.setVisibility(View.VISIBLE);
        mLayout.setVisibility(View.VISIBLE);

    }

}

谢谢

1 个答案:

答案 0 :(得分:0)

感谢我通过添加另一个if语句来解决它,该语句检查代码是否等于我的朋友代码

 if (model.getFrom().equals(myId))
                {
                    holder.fillSentMessage(Image,model.getMessages());
                } else if (model.getFrom().equals(friendId))
                {
                    holder.fillReceivedMessage("",model.getMessages());
                }
                holder.showElements();}