допустим, у меня есть переменная isBlue = true. Если это правда, я хочу установить для моего вновь добавленного текста элемента RecyclerView синий цвет. Если значение равно false, я хочу установить для моего вновь добавленного текста элемента RecyclerView зеленый цвет.
if(isBlue){
dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.redColor))
}else{
dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.greenColor))
}
}
Я попытался добавить этот код в onBind (), но он закрашивает весь текст в заданный цвет, перерисовывая предыдущие тексты элементов RecyclerView, так что в итоге все элементы RecyclerView отображаются синим или зеленым цветом.
private inner class SubjectDateHolder(view:View) : RecyclerView.ViewHolder(view), View.OnClickListener{
private lateinit var date : Date
private val dateTextView :TextView = itemView.findViewById(R.id.text_view_subject_date)
fun bind(date : Date){
this.date = date
dateTextView.text = SimpleDateFormat("d.MMMM ''y. EEEE").format(this.date)
}
}
private inner class SubjectDateAdapter(var dates : List<Date>) : RecyclerView.Adapter<SubjectDateHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubjectDateHolder {
val view = layoutInflater.inflate(R.layout.list_item_date, parent, false)
return SubjectDateHolder(view)
}
override fun getItemCount(): Int {
return dates.size
}
override fun onBindViewHolder(holder: SubjectDateHolder, position: Int) {
val date = dates[position]
holder.bind(date)
}
}
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffffff">
<TextView
android:id="@+id/text_view_subject_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="20sp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>