Android 动态设置recyclerview/cardview背景色

Android 动态设置recyclerview/cardview背景色,android,android-recyclerview,android-cardview,android-viewholder,android-background,Android,Android Recyclerview,Android Cardview,Android Viewholder,Android Background,我想知道是否有可能改变每个回收者视图项目的背景颜色,如果该项目包含某个单词。例如,我的自定义项目包含4个文本视图和1个复选框,如果项目包含单词“dead”,我希望背景颜色为浅棕色,如果项目包含“bench”,则背景颜色为红色。。等等。。有办法做到这一点吗 以下是my item.xml: <android.support.v7.widget.CardView android:id="@+id/card_view" xmlns:android="http://schemas.android.c

我想知道是否有可能改变每个回收者视图项目的背景颜色,如果该项目包含某个单词。例如,我的自定义项目包含4个文本视图和1个复选框,如果项目包含单词“dead”,我希望背景颜色为浅棕色,如果项目包含“bench”,则背景颜色为红色。。等等。。有办法做到这一点吗

以下是my item.xml:

<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="10dp" >


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Squat"
    android:textSize="20sp"
    android:paddingStart="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="50dp"
    android:id="@+id/txtExercise"

    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="%"
    android:textSize="20sp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:id="@+id/txtPercentage"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/txtReps"
    android:layout_toStartOf="@+id/txtReps"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Reps"
    android:paddingRight="10dp"
    android:paddingLeft="10dp"
    android:textSize="20sp"
    android:id="@+id/txtReps"
    android:layout_alignTop="@+id/check1"
    android:layout_toLeftOf="@+id/txtWeight"
    android:layout_toStartOf="@+id/txtWeight"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Weight"
    android:paddingRight="10dp"
    android:paddingLeft="20dp"
    android:textSize="20sp"
    android:id="@+id/txtWeight"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/check1"
    android:layout_toStartOf="@+id/check1"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/check1"
    android:paddingRight="10dp"
    android:checked="false"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

</RelativeLayout>

我的回收者观点:

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {

Context mContext;
ArrayList<Workout> workout;
SharedPreferences prefs;
int firstSecondOrThird;


public MyRecyclerAdapter(Context context, ArrayList<Workout> workout, int thePosition) {
    mContext = context;
    this.workout = workout;
    this.firstSecondOrThird = thePosition;
}
// INITIALIZE HOLDER
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.workout_item, null);
    MyViewHolder holder = new MyViewHolder(view);

    return holder;
}

//BIND DATA TO VIEWS
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    holder.exercise.setText(workout.get(position).getExercise());
    holder.percent.setText(workout.get(position).getPercent());
    holder.reps.setText(workout.get(position).getReps());
    holder.weight.setText(workout.get(position).getWeight());
    holder.check1.setOnCheckedChangeListener(null);
    final Workout isCheck = workout.get(position);
    holder.check1.setChecked(isCheck.isCheck1());
    prefs = mContext.getSharedPreferences("checkstate", Context.MODE_PRIVATE);
    holder.check1.setChecked(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false));
    isCheck.setCheck1(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false));

    holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            isCheck.setCheck1(isChecked);
            prefs.edit().putBoolean(firstSecondOrThird+"checkState"+position, isChecked).apply();
        }
    });

}

@Override
public int getItemCount() {
    return workout.size();
}
}
公共类MyRecyclerAdapter扩展了RecyclerView.Adapter{
语境;
ArrayList训练;
共享引用优先权;
int第一第二第三;
公共MyRecyclerAdapter(上下文、阵列列表训练、点位置){
mContext=上下文;
这个。锻炼=锻炼;
this.firstSecondOrThird=位置;
}
//初始化保持器
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.workout\u项,空);
MyViewHolder=新的MyViewHolder(视图);
报税表持有人;
}
//将数据绑定到视图
@凌驾
公共无效onBindViewHolder(MyViewHolder,最终整数位置){
holder.exercise.setText(workout.get(position.getExercise());
holder.percent.setText(workout.get(position.getPercent());
holder.reps.setText(workout.get(position.getReps());
holder.weight.setText(workout.get(position.getWeight());
holder.check1.setOnCheckedChangeListener(空);
最终训练isCheck=训练。获取(位置);
holder.check1.setChecked(isCheck.isCheck1());
prefs=mContext.getSharedReferences(“checkstate”,Context.MODE\u PRIVATE);
holder.check1.setChecked(prefs.getBoolean(firstSecondOrThird+“checkState”+位置,false));
isCheck.setCheck1(prefs.getBoolean(firstSecondOrThird+“checkState”+位置,false));
holder.check1.setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
isCheck.setCheck1(isChecked);
prefs.edit().putBoolean(firstSecondOrThird+“checkState”+位置,已选中);
}
});
}
@凌驾
public int getItemCount(){
返回训练。大小();
}
}

谢谢

在基本
ViewHolder
类中,
itemView
是公共的,因此在
onBindViewHolder()
方法中,可以检查所需的颜色并进行设置

    int colorResId = R.color.default;
    if (workout.get(position).getExercise().contains("bench") {
        colorResId = R.color.red;
    //  more tests for which color to use
    }
    int color = getResources.getColor(colorResId, context);
    ((CardView) holder.itemView).setCardBackgroundColor(color);

在基本的
ViewHolder
类中,
itemView
是公共的,因此在
onBindViewHolder()
方法中,您可以检查所需的颜色并进行设置

    int colorResId = R.color.default;
    if (workout.get(position).getExercise().contains("bench") {
        colorResId = R.color.red;
    //  more tests for which color to use
    }
    int color = getResources.getColor(colorResId, context);
    ((CardView) holder.itemView).setCardBackgroundColor(color);

因此,您的ViewHolder有一个与之关联的
视图。其中包含您发布的xml文件。现在,在您的
onBindViewHolder
方法中,您将数据绑定到您的
MyViewHolder
。通过调用
holder.itemView
返回
View
对象,可以获取
MyViewHolder
视图

View itemView = holder.itemView;
现在,您可以通过调用
itemView上的
findViewById
获取对
TextView
按钮等的引用

因此,例如,如果您的xml文件中有一个id为android:id=“@+id\myTextView
TextView
,您可以在
onBindViewHolder
中执行以下操作

public void onBindViewHolder(MyViewHolder holder, final int position) {
    //..... Other stuff
    View itemView = holder.itemView;
    TextView myTextView = (TextView) itemView.findViewById(R.id.myTextView);
    myTextView.setBackgroundResource(context.getResources().getColor(android.R.color.holo_red_dark));
   //...... Other stuff
}

因此,您的ViewHolder有一个与之关联的
视图
。该视图通过您发布的xml文件膨胀。现在在
onBindViewHolder
方法中,您可以将数据绑定到
MyViewHolder
。您可以通过调用
holder.itemView
来获取
MyViewHolder
视图,该视图返回
 查看
对象

View itemView = holder.itemView;
现在,您可以通过调用
itemView上的
findViewById
获取对
TextView
按钮等的引用

因此,例如,如果您的xml文件中有一个id为android:id=“@+id\myTextView
TextView
,您可以在
onBindViewHolder
中执行以下操作

public void onBindViewHolder(MyViewHolder holder, final int position) {
    //..... Other stuff
    View itemView = holder.itemView;
    TextView myTextView = (TextView) itemView.findViewById(R.id.myTextView);
    myTextView.setBackgroundResource(context.getResources().getColor(android.R.color.holo_red_dark));
   //...... Other stuff
}

要添加此字段,您可以在
训练
课程中添加另一个字段。类似于
int workOutColor
创建getter和setter,然后在代码中只需执行
holder.itemView.setBackgroundResource(workout.get(position.getWorkOutColor())这是非常干净的,让你的类做逻辑。谢谢,这是很好的工作,不幸的是,当我使用它时,它基本上摆脱了cardview属性,角不再弯曲。有什么办法可以解决这个问题吗?看起来颜色也在不断变化。。有时我会运行应用程序,它工作正常。。其他时候,它会将颜色更改为我的应用程序中不存在的颜色?哦,是的,颜色资源有一个错误,我更新了我的答案以向您展示如何解决它。getResources不起作用,它说无法解决此问题。若要添加此项,您可以在
训练
类中添加另一个字段。类似于
int workOutColor
创建getter和setter,然后在代码中只需执行
holder.itemView.setBackgroundResource(workout.get(position.getWorkOutColor())这是非常干净的,让你的类做逻辑。谢谢,这是很好的工作,不幸的是,当我使用它时,它基本上摆脱了cardview属性,角不再弯曲。有什么办法可以解决这个问题吗?看起来颜色也在不断变化。。有时我会运行应用程序,它工作正常。。其他时候,它会将颜色更改为我的应用程序中不存在的颜色?哦,是的,颜色资源有一个错误,我更新了我的答案,告诉你如何解决它。getResources不起作用,它说它无法解决。我正在尝试解决RecyclerView中的一个问题,并在上面找到了你的答案。对于这个问题,如果您有任何想法或想法,我将不胜感激:我正在尝试解决RecyclerVi中的一个问题