У меня есть RecyclerView
в Android, и я хочу, чтобы каждая ячейка (или элемент, если хотите) имела уникальный фон, который является объектом GradientDrawable
. Вот соответствующие фрагменты кода:
public static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
private LinearLayout mEmotionCell;
public MyViewHolder(View view) {
super(view);
mTextView = view.findViewById(R.id.emotionName);
mEmotionCell = view.findViewById(R.id.emotionCell);
}
public void getItem() {
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
final EmotionResponse thisEmotion = mDataSet.get(position);
final int index = position;
Tone tone = new EmotionMap().map(mDataSet.get(position).getName());
((MyViewHolder)holder).mTextView.setText(thisEmotion.getName());
((MyViewHolder)holder).mEmotionCell.setBackground(new GradientDrawable(GradientDrawable.
Orientation.LEFT_RIGHT, new int[] {tone.getStartColor(), tone.getEndColor()}));
}
, где Tone
- это созданный мной пользовательский объект, а Tone.getStartColor()
и Tone.getEndColor()
возвращают целые числа (их регистрация показывает значения, которые я ожидал получить, поэтому они не т вопрос). При запуске кода фон каждой ячейки остается пустым белым. Странно то, что когда я пытаюсь сделать mEmotionCell.setBackgroundColor(some color)
, это работает. Так же и mEmotionCell.setBackgroundResource(some drawable resource)
. Только setBackground
, кажется, доставляет мне неприятности. Любые идеи? Вот XML, которое добавлено в каждую ячейку:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/emotionCell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/emotionName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textSize="36sp"
android:textColor="#000000"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_margin="20dp"/>
</RelativeLayout>
</LinearLayout>
Заранее спасибо!