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


我想更改通过recyclerView显示的所选项目的颜色。我只是不知道所选项目的位置。我刚刚在互联网上看到了解决方案,但没有得到,或者它们大多是Java代码。我正在开发中。请让我找到所单击项目的确切位置,以便我可以将条件放置在else函数中

最佳答案

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

10-01 02:22