Guest User

Untitled

a guest
Mar 25th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.05 KB | None | 0 0
  1. class TodoListAdapter :
  2.     ListAdapter<ToDoModel, TodoListAdapter.ToDoViewHolder>(ToDoListComparator()) {
  3.     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ToDoViewHolder {
  4.         return ToDoViewHolder.create(parent)
  5.     }
  6.  
  7.     override fun onBindViewHolder(holder: ToDoViewHolder, position: Int) {
  8.         holder.bind(getItem(position))
  9.     }
  10.  
  11.     class ToDoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  12.         private val titleItem: TextView = itemView.findViewById(R.id.title_txt)
  13.         private val descriptionItem: TextView = itemView.findViewById(R.id.description_txt)
  14.         private val priorityCircle: View = itemView.findViewById(R.id.priority_indicator)
  15.         private val priorityItem: CardView = itemView.findViewById(R.id.priority_card)
  16.  
  17.         fun bind(toDoModel: ToDoModel) {
  18.             titleItem.text = toDoModel.title
  19.             descriptionItem.text = toDoModel.description
  20.             descriptionItem.setTextColor(ContextCompat.getColor(itemView.context, R.color.red))
  21.             val color = ContextCompat.getColor(itemView.context, R.color.red)
  22.             Log.d("LOH", "bind: $color")
  23.             //priorityCircle.setBackgroundColor(ContextCompat.getColor(itemView.context, R.color.red))
  24.  
  25.             when (toDoModel.priority) {
  26.                 Priority.High -> priorityItem.backgroundTintList = ColorStateList.valueOf(color)
  27.                 Priority.Medium -> priorityItem.setCardBackgroundColor(
  28.                     ContextCompat.getColor(
  29.                         itemView.context,
  30.                         R.color.yellow
  31.                     )
  32.                 )
  33.                 else -> priorityItem.setCardBackgroundColor(
  34.                     ContextCompat.getColor(
  35.                         itemView.context,
  36.                         R.color.green
  37.                     )
  38.                 )
  39.             }
  40.  
  41.             when (toDoModel.priority) {
  42.                 Priority.High -> priorityCircle.backgroundTintList = ColorStateList.valueOf(color)
  43.                 Priority.Medium -> priorityCircle.backgroundTintList =
  44.                     ColorStateList.valueOf(ContextCompat.getColor(itemView.context, R.color.yellow))
  45.                 else -> priorityCircle.setBackgroundColor(
  46.                     ContextCompat.getColor(
  47.                         itemView.context,
  48.                         R.color.green
  49.                     )
  50.                 )
  51.             }
  52.         }
  53.  
  54.         companion object {
  55.             fun create(parent: ViewGroup): ToDoViewHolder {
  56.                 val view = LayoutInflater.from(parent.context)
  57.                     .inflate(R.layout.row_layout, parent, false)
  58.                 return ToDoViewHolder(view)
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. class ToDoListComparator : DiffUtil.ItemCallback<ToDoModel>() {
  65.     override fun areItemsTheSame(oldItem: ToDoModel, newItem: ToDoModel): Boolean {
  66.         return oldItem == newItem
  67.     }
  68.  
  69.     override fun areContentsTheSame(oldItem: ToDoModel, newItem: ToDoModel): Boolean {
  70.         return oldItem.title == newItem.title
  71.     }
  72. }
Add Comment
Please, Sign In to add comment