动态设置 recyclerview/cardview 背景颜色

Setting recyclerview/cardview background colour dynamically

我想知道是否可以更改每个回收站视图项目的背景颜色(如果该项目包含某个词)。例如,我的自定义项目包含 4 个文本视图和 1 个复选框,如果项目包含单词 "dead",我希望背景颜色为浅棕色,如果包含 "bench".. ect ect.. 则为红色有办法吗?

这是我的 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();
}
}

谢谢!

在基础 ViewHolder class 中,itemView 是 public,因此在您的 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 有一个与之关联的 View。其中包含您发布的 xml 文件。现在,在您的 onBindViewHolder 方法中,您将数据绑定到 MyViewHolder。您可以通过调用 holder.itemView which returns 一个 View 对象来获取 MyViewHolderView

View itemView = holder.itemView;

现在,您可以通过在 itemView

上调用 findViewById 来获取对 TextViewButton 等的引用

例如,如果您的 xml 文件中有一个 TextView,ID 为 android:id="@+id\myTextView,您可以在 onBindViewHolder、[=26= 中执行以下操作]

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
}