Change text color of only selected item on RecyclerView android Kotlin

一世执手 提交于 2020-04-18 05:45:45

问题


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. 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


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



回答2:


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

        }
    }


来源:https://stackoverflow.com/questions/61156954/change-text-color-of-only-selected-item-on-recyclerview-android-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!