Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android onBindViewHolder逻辑执行不一致_Android_Android Recyclerview - Fatal编程技术网

Android onBindViewHolder逻辑执行不一致

Android onBindViewHolder逻辑执行不一致,android,android-recyclerview,Android,Android Recyclerview,我正在进行一项传统的聊天活动,根据消息作者的Firebase ID是否是登录用户来设置消息样式。当活动最初启动并创建视图时,消息被赋予适当的外观,但当添加新消息时,消息被赋予错误的外观。尽管作者信息相同,一些旧邮件甚至从一种样式切换到另一种样式: 用户1: 用户2: onBindViewHolder代码: @Override public void onBindViewHolder(@NonNull DMViewHolder holder, int position) {

我正在进行一项传统的聊天活动,根据消息作者的Firebase ID是否是登录用户来设置消息样式。当活动最初启动并创建视图时,消息被赋予适当的外观,但当添加新消息时,消息被赋予错误的外观。尽管作者信息相同,一些旧邮件甚至从一种样式切换到另一种样式:

用户1:

用户2:

onBindViewHolder代码:

 @Override
    public void onBindViewHolder(@NonNull DMViewHolder holder, int position) {




        Message message = messageList.get(position);

        String signedInUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();

        boolean isSignedInUser = message.getAuthorUID().equals(signedInUserID);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a");

        holder.message.setText(message.getMessage());
        holder.date.setText(simpleDateFormat.format(message.getTimestamp()));

        if (isSignedInUser) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_END);

            holder.textContainer.setLayoutParams(params);
        } else {

            holder.textContainer.setBackgroundResource(R.drawable.partner_message_text_background);
        }


    }

此代码中存在问题:

只有当您的
if
条件计算为true时,才可以更改
LayoutParams
。当
if
的计算结果为false时,需要将其设置回原位。类似地,您可以在
if
语句的两个分支中调用
setBackgroundResource()
,也可以在
if
之外调用它

if (isSignedInUser) {
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_END);
    holder.textContainer.setLayoutParams(params);

    holder.textContainer.setBackgroundResource(R.drawable.some_other_background);
} else {
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_START);
    holder.textContainer.setLayoutParams(params);

    holder.textContainer.setBackgroundResource(R.drawable.partner_message_text_background);
}

使用ListView或RecyclerView时,必须记住每次调用视图时都要更新视图的每个部分。。。当视图被回收时,仅更新视图的一部分将导致类似这样的问题。

问题出现在以下代码中:

只有当您的
if
条件计算为true时,才可以更改
LayoutParams
。当
if
的计算结果为false时,需要将其设置回原位。类似地,您可以在
if
语句的两个分支中调用
setBackgroundResource()
,也可以在
if
之外调用它

if (isSignedInUser) {
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_END);
    holder.textContainer.setLayoutParams(params);

    holder.textContainer.setBackgroundResource(R.drawable.some_other_background);
} else {
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_START);
    holder.textContainer.setLayoutParams(params);

    holder.textContainer.setBackgroundResource(R.drawable.partner_message_text_background);
}

使用ListView或RecyclerView时,必须记住每次调用视图时都要更新视图的每个部分。。。当视图被回收时,仅更新视图的一部分会导致类似这样的问题。

Ah是的。这是有道理的。谢谢。啊,是的。这是有道理的。非常感谢。