gpt4 book ai didi

java - 在 RecyclerView android Kotlin 上仅更改所选项目的文本颜色

转载 作者:行者123 更新时间:2023-12-02 08:44:40 26 4
gpt4 key购买 nike

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代码。我是开发新手。让我找到单击项目的确切位置,以便我可以将条件放入 if 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)
}

关于java - 在 RecyclerView android Kotlin 上仅更改所选项目的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61156954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com