简体   繁体   English

更改 RecyclerView 上仅选定项目的文本颜色 android Kotlin

[英]Change text color of only selected item on RecyclerView android Kotlin

class CustomeAdapterForTopics(
    val ctx: Context,
    var clickListener: OnTopicClick,
    val items: ArrayList<ModelForTopics>
) : RecyclerView.Adapter<TopicViewHolder>() {

    override fun onBindViewHolder(holder: TopicViewHolder, position: Int) {

        val user: ModelForTopics = items[position]
        holder.textViewName.text = user.name
        holder.initilise(items.get(position), clickListener)

//        if() {
//            holder.textViewName.setTextColor(Color.parseColor("#FFA07A"));
//
//
//        } else {
//            holder.textViewName.setTextColor(Color.parseColor("#FFBA5F")); 
//
//        }
    }


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

    override fun getItemCount(): Int {
        return items.size
    }


}


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
//        textViewName.setTextColor(Color.parseColor("#25375F"))

        itemView.setOnClickListener {
            action.onItemClick(items, adapterPosition)
//            textViewName.setTextColor(Color.parseColor("#FFBA5F"))
//            if() {
//                textViewName.setTextColor(Color.parseColor("#FFBA5F"))
//            }
//            else{
//                textViewName.setTextColor(Color.parseColor("#25375F"))
//            }

        }

    }
}

interface OnTopicClick {
    fun onItemClick(items: ModelForTopics, position: Int)
}

I want to change the color for selected item which is shown through recyclerView.我想更改通过 recyclerView 显示的选定项目的颜色。 I just don't get the position of selected item.我只是没有得到所选项目的 position。 I just saw the solution on internet but didn't get it, or they are mostly of java code.我只是在网上看到了解决方案,但没有得到它,或者它们主要是 java 代码。 Im new on development.我是新开发的。 Just let me find the exact position of clicked item so i can put condition in if else function让我找到点击项目的确切 position 以便我可以设置条件,否则 function

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)
}

You need to add isSelected field to ModelForTopics.您需要将 isSelected 字段添加到 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()
            }

        }
    }

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>)
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM