【问题标题】:CardView changes color when expandedCardView 在展开时改变颜色
【发布时间】:2021-01-11 11:46:00
【问题描述】:

最近我在使用 CardViews 时遇到了一种非常奇怪的行为。场景如下:我有一个使用 CardViews 作为项目的 RecyclerView。数据是动态的,并且是在 RecyclerView 被填充之前从 Internet 获取的。 每个 CardView 都分配给 Job.java 的一个实例。 Job 有一个 isPending()-boolean,它返回 true 或 false(奇迹)。卡片的背景颜色(和文本)由 isPending()-boolean 确定,就像这样

holder.cv.setCardBackgroundColor(jobs.get(position).isPending()?Color.parseColor("#FFDF72"):holder.cv.getCardBackgroundColor().getDefaultColor());

到目前为止,它就像一个魅力。 然后我想添加一个 onClickListener 来扩展点击的卡片,然后调用 notifyItemChanged(position); 这也可以正常工作。让我们来看看奇怪的行为。
当我展开一张白卡(未挂起)并关闭它时,一切正常。当我对一张黄牌(待定)做同样的事情时,一切都很好。 但是 选择一张黄牌后,每当我打开一张白卡时,它会变成黄色,扩大时会变黄,再次缩小时会变回白色。 我真的不知道它可能是什么,因为我尝试调试并且在卡片设置颜色时,布尔值“isPending()”总是正确的。

CardView 的 xml 代码:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:foreground="?android:attr/selectableItemBackground"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:orientation="vertical"
    app:cardCornerRadius="2dp"
    app:cardElevation="6dp"
    android:clickable="true"
    android:focusable="true">

RVAdapter 的 onBindViewHolder 方法:

if(jobs.get(position).isPending()) { //the mentioned method expanded due to debug reasons
            System.out.println("hsdajdshakl");
            holder.cv.setCardBackgroundColor(Color.parseColor("#FFDF72"));
        } else holder.cv.setCardBackgroundColor(holder.cv.getCardBackgroundColor().getDefaultColor());
// ... irrelevant code
holder.cv.findViewById(R.id.job_add).setBackgroundColor(holder.cv.getCardBackgroundColor().getDefaultColor());
        holder.cv.findViewById(R.id.job_decline).setBackgroundColor(jobs.get(position).isPending()?Color.parseColor("#FFDF72"):holder.cv.getCardBackgroundColor().getDefaultColor());
        holder.cv.findViewById(R.id.job_personen).setBackgroundColor(jobs.get(position).isPending()?Color.parseColor("#FFDF72"):holder.cv.getCardBackgroundColor().getDefaultColor());

holder.itemView.setActivated(isExpanded);

        holder.itemView.setOnClickListener(v -> {
            mExpandedPosition = isExpanded?-1:position;
            //notifyItemChanged(previousExpandedPosition);
            //jobs.get(position).setPending(jobs.get(position).isPending()); for testing
            notifyItemChanged(position);
        });

【问题讨论】:

  • 看起来holder.cv.getCardBackgroundColor().getDefaultColor() 是你的错误,明确传递默认颜色,就像你这样做Color.parseColor("#FFDF72")
  • @rahatrau 亲爱的,我不知道发生了什么事。首先它有效,谢谢。我真的尝试了很多东西,包括改变它,但它没有奏效。不知何故,我也必须更改其他参数,所以它没有。另一个问题,有没有办法应用在 xml 中设置的颜色? (我应该删除这个线程)
  • 所以很明显这个错误来自那里。但我不明白为什么得到的 CardBackgroundColor 会返回黄色,尽管提到的卡片以前从未是黄色的。

标签: java android android-cardview


【解决方案1】:
holder.cv.findViewById(R.id.job_decline).setBackgroundColor(jobs.get(position).isPending()?Color.parseColor("#FFDF72"):holder.cv.getCardBackgroundColor().getDefaultColor());
    holder.cv.findViewById(R.id.job_personen).setBackgroundColor(jobs.get(position).isPending()?Color.parseColor("#FFDF72"):holder.cv.getCardBackgroundColor().getDefaultColor());

cv.getCardBackgroundColor() 将返回一个ColorStateList,由于您没有使用 statelist 并且没有在 XML 或代码中指定任何 statelist,getDefaultColor() 颜色将返回与cardBackgroundColor 相同的颜色。

recyclerview 的诀窍在于它可能会或可能不会对特定位置使用相同的视图,假设有 10 个项目,因此对于第 6 个项目,它可能使用用于第 3 个项目的视图副本,假设您的第 3 个项目是黄色的,那么第 6 个项目将得到 cv.getCardBackgroundColor(),调用 getDefaultColor() 将返回黄色,原因如上所述。

解决办法是,要么指定ColorStateList,为不同的状态设置颜色,要么直接通过解析颜色资源来设置颜色,

【讨论】:

  • 非常感谢。让我下一步检查 ColorStateLists。
猜你喜欢
  • 1970-01-01
  • 2018-12-02
  • 2017-12-02
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多