动态设置recyclerview / cardview背景颜色

时间:2016-08-08 19:05:36

标签: android android-recyclerview android-cardview android-viewholder android-background

我想知道如果项目包含某个单词,是否可以更改每个回收站视图项目的背景颜色。例如,我的自定义项目包含4个textviews和1个复选框,如果项目包含单词" dead",则为背景颜色为浅棕色;如果包含" bench"则为红色。等等..有没有办法做到这一点?

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

谢谢!

2 个答案:

答案 0 :(得分:3)

在基础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);

答案 1 :(得分:1)

因此,您的ViewHolder与View相关联。哪个是您发布的xml文件。现在,在onBindViewHolder方法中,您可以将数据绑定到MyViewHolder。您可以通过调用返回View对象的MyViewHolder来获取holder.itemView的{​​{1}}。

View

现在,您可以通过View itemView = holder.itemView; 上的TextView

来获取对ButtonfindViewById等的引用

例如,如果您的xml文件中有itemView且标识为TextView,则可以在android:id="@+id\myTextView中执行以下操作,

onBindViewHolder