gpt4 book ai didi

java - 回收适配器白化中行的替代背景颜色会破坏动画

转载 作者:行者123 更新时间:2023-12-01 17:29:08 25 4
gpt4 key购买 nike

我想要具有替代颜色的 RecyclerView,当我单击每一行时,我会看到一个磁带动画。

现在磁带动画开始工作了。但是当我想添加代码来更改颜色时,我的点击动画不再起作用。

这是我的行代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:forceDarkAllowed="false"
android:background="?attr/selectableItemBackground"//Here is where my tap animation is set>

<ImageView
android:layout_width="@android:dimen/app_icon_size"
android:layout_height="@android:dimen/app_icon_size" tools:srcCompat="@tools:sample/avatars"
android:id="@+id/contactImg"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp"
/>
<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/titleView"
android:textAppearance="@style/TextAppearance.AppCompat.Large" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/subTitleView" android:layout_marginTop="16dp"
app:layout_constraintStart_toEndOf="@+id/contactImg" android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"/>
<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/subTitleView"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/titleView"
android:layout_marginBottom="16dp" app:layout_constraintStart_toEndOf="@+id/contactImg"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的回收器适配器类中的备用颜色代码:

@Override
public void onBindViewHolder(@NonNull ViewHoler holder, int position) {
holder.titleView.setText(ContactList.get(position));
holder.subTitleView.setText(String.valueOf(position));
if (position%2==1){ //check if the position of row is odd
holder.itemView.setBackgroundColor(appResources.getColor(R.color.lightDark)); //if is odd I apply one color
}else{
holder.itemView.setBackgroundColor(Color.WHITE);//if isn't odd I aply another color
}
}

最佳答案

这应该是因为您同时设置了带有动画和颜色的背景。通过 onBindViewHolder 方法中的 setBackgroundColor 方法,您可以用您的颜色替换动画。删除 android:background="?attr/selectableItemBackground" 并在 onBindViewHolder 方法中执行您想要设置的任何操作。

关于java - 回收适配器白化中行的替代背景颜色会破坏动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61157177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com