本文介绍了更改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)
}
我想更改通过recyclerView显示的所选项目的颜色.我只是不知道所选项目的位置.我刚刚在Internet上看到了解决方案,但没有得到,或者它们大多是Java代码.我正在开发中.只要让我找到所单击项目的确切位置,以便可以将条件放置在if else函数中
I want to change the color for selected item which is shown through recyclerView. I just don't get the position of selected item. I just saw the solution on internet but didn't get it, or they are mostly of java code. Im new on development. Just let me find the exact position of clicked item so i can put condition in if else 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)
}
这篇关于更改RecyclerView android Kotlin上仅所选项目的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!