RecyclerView重置聊天背景

时间:2018-07-23 07:23:29

标签: android mobile android-recyclerview

所以我正在开发一个带有Firebase后端的移动聊天应用程序。我遇到的问题是,当我为某些...但不是所有的recyclerview项目发送消息时,资源背景会发生变化。其外观如下:

enter image description here

这是我的适配器中的onBindViewHolder:

 @Override
    public void onBindViewHolder(@NonNull ChatViewHolders chatViewHolders, int i) {
        chatViewHolders.mMessage.setText(chatList.get(i).getMessage());
        chatViewHolders.mTimestamp.setText(DateFormat.format("dd-MM-yyyy (HH:mm)", chatList.get(i).getTimestamp()));
        //IF IT'S THE USER
        if(chatList.get(i).getCurrentUser()){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.weight = 1.0f;
            params.gravity = Gravity.END;
            chatViewHolders.mMessage.setGravity(Gravity.END);
            chatViewHolders.mTimestamp.setGravity(Gravity.END);
            chatViewHolders.mMessage.setTextColor(Color.parseColor("#404040"));
            chatViewHolders.mContainer2.setLayoutParams(params);
            chatViewHolders.mContainer.setLayoutParams(params);
            chatViewHolders.mMessage.setBackgroundResource(R.drawable.bubble2);
        }
        //IF ITS THE MATCH
        else if (!chatList.get(i).getCurrentUser()){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.weight = 1.0f;
            params.gravity = Gravity.START;
            chatViewHolders.mMessage.setGravity(Gravity.START);
            chatViewHolders.mMessage.setTextColor(Color.parseColor("#FFFFFF"));

            chatViewHolders.mContainer2.setLayoutParams(params);
            chatViewHolders.mContainer.setLayoutParams(params);
            chatViewHolders.mContainer.setBackgroundResource(R.drawable.bubble);

        }

我认为问题出在OnBindViewHolder方法之内,但我似乎无法取得任何进展?

1 个答案:

答案 0 :(得分:1)

    if(chatList.get(i).getCurrentUser()){
        ...
        chatViewHolders.mMessage.setBackgroundResource(R.drawable.bubble2);
    } else if (!chatList.get(i).getCurrentUser()){
        ...
        // chatViewHolders.mContainer.setBackgroundResource(R.drawable.bubble);
        chatViewHolders.mMessage.setBackgroundResource(R.drawable.bubble);
    }

仅更改chatViewHolders.mMessage的背景