我想使用其他颜色的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方法中设置的所有操作。