Skip to content Skip to sidebar Skip to footer

Change Text Color Of Only Selected Item On RecyclerView Android Kotlin

class CustomeAdapterForTopics( val ctx: Context, var clickListener: OnTopicClick, val items: ArrayList ) : RecyclerView.Adapter

Solution 1:

class ModelForTopics() {
    // ...
    var isSelected: Boolean = false
}

class CustomeAdapterForTopics(
    var clickListener: OnTopicClick,
    private val items: List<ModelForTopics>
) : RecyclerView.Adapter<TopicViewHolder>() {
    var selectedItemIndex = -1

    override fun onBindViewHolder(holder: TopicViewHolder, position: Int) {
        val item = items[position]
        holder.textViewName.text = item.name
        if (item.isSelected) {
            holder.textViewName.setTextColor(Color.parseColor("#FFBA5F"))
        } else {
            holder.textViewName.setTextColor(Color.parseColor("#25375F"))
        }
        holder.itemView.setOnClickListener {
            clickListener.onItemClick(item, position)
            item.isSelected = true
            if (selectedItemIndex != -1)
                items[selectedItemIndex].isSelected = false
            selectedItemIndex = position
            notifyDataSetChanged()
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopicViewHolder =
        with(LayoutInflater.from(parent.context).inflate(R.layout.topics_row, parent, false)) {
            TopicViewHolder(this)
        }

    override fun getItemCount() = items.size
}


class TopicViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val textViewName: TextView = itemView.findViewById(R.id.textView)
}

Solution 2:

This might help you

class MyClassesAdapter(
private val btnNames: List<String>,
private val listener: OnItemClickListener
) : RecyclerView.Adapter<MyClassesAdapter.MyClassViewHolder>() {

private var selectedPos = -1
private var prevSelectedPos = -1
private lateinit var context: Context

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyClassViewHolder {

    context = parent.context

    val binding = RvItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return MyClassViewHolder(binding)
}

override fun onBindViewHolder(holder: MyClassViewHolder, position: Int) {
    holder.bind(btnNames[position])
    holder.selectedOption(selectedPos, position)
}

override fun getItemCount(): Int {
    return if (btnNames.isEmpty()) 0
    else
        btnNames.size
}

inner class MyClassViewHolder(private val binding: RvItemBinding) :
    RecyclerView.ViewHolder(binding.root), View.OnClickListener {
    fun bind(btnText: String) {
        binding.btnItem.text = btnText
    }
    init {
        binding.btnItem.setOnClickListener(this)
    }

    override fun onClick(v: View?) {

        if (selectedPos >= 0) {
            notifyItemChanged(selectedPos)
        }
        selectedPos = adapterPosition
        notifyItemChanged(selectedPos)

        listener.onClassItemClick(adapterPosition, btnNames)
    }

    fun selectedOption(selectedPos: Int, position: Int) {

        if (position == prevSelectedPos) {
            binding.btnItem.background.setTint(ContextCompat.getColor(context, R.color.grey))
            binding.btnItem.setTextColor(ContextCompat.getColor(context, R.color.black))
            prevSelectedPos = -1
            return
        }

        if (selectedPos == position) {
            binding.btnItem.background.setTint(ContextCompat.getColor(context, R.color.sky_blue))
            binding.btnItem.setTextColor(ContextCompat.getColor(context, R.color.white))
            prevSelectedPos = position
        } else {
            binding.btnItem.background.setTint(ContextCompat.getColor(context, R.color.grey))
            binding.btnItem.setTextColor(ContextCompat.getColor(context, R.color.black))
        }
    }
}

    interface OnItemClickListener {
        fun onClassItemClick(pos: Int, btns: List<String>)
    }

}

Solution 3:

You need to add isSelected field to ModelForTopics.

class ModelForTopics {
    ...
    public boolean isSelected = false;
}

Then you need to add this code

class TopicViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    //    var itemViewList: List<View> = ArrayList()
        val textViewName = itemView.findViewById(R.id.textView) as TextView
    //    itemViewList.add(itemView);
        fun initilise(items: ModelForTopics, action: OnTopicClick) {
            textViewName.text = items.name

            if(items.isSelected) {
                textViewName.setTextColor(Color.parseColor("#FFBA5F"))
            } else {
                textViewName.setTextColor(Color.parseColor("#25375F"))
            }

            itemView.setOnClickListener {
                action.onItemClick(items, adapterPosition)
                item.isSelected = !item.isSelected
                notifyDataSetChanged()
            }

        }
    }

Post a Comment for "Change Text Color Of Only Selected Item On RecyclerView Android Kotlin"