Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TodoListAdapter :
- ListAdapter<ToDoModel, TodoListAdapter.ToDoViewHolder>(ToDoListComparator()) {
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ToDoViewHolder {
- return ToDoViewHolder.create(parent)
- }
- override fun onBindViewHolder(holder: ToDoViewHolder, position: Int) {
- holder.bind(getItem(position))
- }
- class ToDoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
- private val titleItem: TextView = itemView.findViewById(R.id.title_txt)
- private val descriptionItem: TextView = itemView.findViewById(R.id.description_txt)
- private val priorityCircle: View = itemView.findViewById(R.id.priority_indicator)
- private val priorityItem: CardView = itemView.findViewById(R.id.priority_card)
- fun bind(toDoModel: ToDoModel) {
- titleItem.text = toDoModel.title
- descriptionItem.text = toDoModel.description
- descriptionItem.setTextColor(ContextCompat.getColor(itemView.context, R.color.red))
- val color = ContextCompat.getColor(itemView.context, R.color.red)
- Log.d("LOH", "bind: $color")
- //priorityCircle.setBackgroundColor(ContextCompat.getColor(itemView.context, R.color.red))
- when (toDoModel.priority) {
- Priority.High -> priorityItem.backgroundTintList = ColorStateList.valueOf(color)
- Priority.Medium -> priorityItem.setCardBackgroundColor(
- ContextCompat.getColor(
- itemView.context,
- R.color.yellow
- )
- )
- else -> priorityItem.setCardBackgroundColor(
- ContextCompat.getColor(
- itemView.context,
- R.color.green
- )
- )
- }
- when (toDoModel.priority) {
- Priority.High -> priorityCircle.backgroundTintList = ColorStateList.valueOf(color)
- Priority.Medium -> priorityCircle.backgroundTintList =
- ColorStateList.valueOf(ContextCompat.getColor(itemView.context, R.color.yellow))
- else -> priorityCircle.setBackgroundColor(
- ContextCompat.getColor(
- itemView.context,
- R.color.green
- )
- )
- }
- }
- companion object {
- fun create(parent: ViewGroup): ToDoViewHolder {
- val view = LayoutInflater.from(parent.context)
- .inflate(R.layout.row_layout, parent, false)
- return ToDoViewHolder(view)
- }
- }
- }
- }
- class ToDoListComparator : DiffUtil.ItemCallback<ToDoModel>() {
- override fun areItemsTheSame(oldItem: ToDoModel, newItem: ToDoModel): Boolean {
- return oldItem == newItem
- }
- override fun areContentsTheSame(oldItem: ToDoModel, newItem: ToDoModel): Boolean {
- return oldItem.title == newItem.title
- }
- }
Add Comment
Please, Sign In to add comment