lets say I have a variable isBlue = true. If it is true I want to set my newly added RecyclerView item text to blue. If it is false I want to set my newly added RecyclerView item text to green.
if(isBlue){
dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.redColor))
}else{
dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.greenColor))
}
}
I have tried adding this code to onBind(), but it paints all the text to the set color, repainting the previous RecyclerView item texts, so I end up with all RecyclerView item text being either blue or green.
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>