Kotlin Recyclerview row item selection background color change

前端 未结 2 1165
独厮守ぢ
独厮守ぢ 2021-01-26 05:26

I am able to change the color of the text and background of row clicked of my recyclerview in my recyclerview.

But my probl

2条回答
  •  无人共我
    2021-01-26 05:45

    After a thorough search on the Internet, I was finally able to solve this problem.
    I put the code step by step and give explanations if needed.

    1 - create new android studio project
    2 - cods for activity_main.xml as below:
    activity_main.xml

    
    
    
        
      
    

    3 - create a layout for recycler view row(item) with name item_list.xml as bellow
    item_list.xml

    
    
    
    
        
    
            
    
            
    
        
    
        
    
      
    

    4 - create data class(Based on the data you want to bind in RecyclerView) as bellow

    My Model(UserModel.kt)

        public data class UserModel(var title:String,var name:String,var isSelected:Boolean=false) 
    

    5 - create Adapter for your RecyclerView as bellow

    CustomAdapter.kt

    class CustomAdapter(private val context: Context, private val list: ArrayList,
    private val listener: OnItemClickListener
                        ) : RecyclerView.Adapter() {
    
        private inner class ViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView),View.OnClickListener {
    
            internal var tvLabel: TextView
            internal var tvName: TextView
    
            init {
                tvLabel = itemView.findViewById(R.id.tv_label) // Initialize your All views prensent in list items
                tvName = itemView.findViewById(R.id.tv_name) // Initialize your All views prensent in list items
                itemView.setOnClickListener(this)
            }
    
            internal fun bind(position: Int) {
                // This method will be called anytime a list item is created or update its data
                //Do your stuff here
                tvLabel.text = list[position].title
                tvName.text = list[position].name
            }
    
            override fun onClick(v: View?) {
                val position:Int = adapterPosition
                if(position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(position,this@CustomAdapter,itemView)
                }
            }
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_list, parent, false))
        }
    
        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if(list[position].isSelected){
                holder.itemView.setBackgroundColor(Color.YELLOW)
                holder.itemView.findViewById(R.id.linear_content).setBackgroundColor(Color.YELLOW)
            } else{
                holder.itemView.setBackgroundColor(Color.WHITE)
                holder.itemView.findViewById(R.id.linear_content).setBackgroundColor(Color.WHITE)
            }
            (holder as ViewHolder).bind(position)
        }
    
        override fun getItemCount(): Int {
            return list.size
        }
    
        interface OnItemClickListener{
            fun onItemClick(position: Int,adapter:CustomAdapter,v:View)
        }
    }  
    

    6 - codes for MainActivity.kt as bellow:
    MainActivity.kt

    class MainActivity : AppCompatActivity(),CustomAdapter.OnItemClickListener {
        private val data = arrayListOf()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main_page)
    
            val recyclerview = findViewById(R.id.recycler_view)
    
            data.add(UserModel(title = "item 1",name = "name 1"))
            data.add(UserModel(title = "item 2",name = "name 2"))
            data.add(UserModel(title = "item 3",name = "name 3"))
            data.add(UserModel(title = "item 4",name = "name 4"))
            data.add(UserModel(title = "item 5",name = "name 5"))
            data.add(UserModel(title = "item 6",name = "name 6"))
            data.add(UserModel(title = "item 1",name = "name 1"))
            data.add(UserModel(title = "item 2",name = "name 2"))
            data.add(UserModel(title = "item 3",name = "name 3"))
            data.add(UserModel(title = "item 4",name = "name 4"))
            data.add(UserModel(title = "item 5",name = "name 5"))
            data.add(UserModel(title = "item 6",name = "name 6"))
     
    
            val adapter = CustomAdapter(this,data,this)
            recyclerview.layoutManager = LinearLayoutManager(this)
            recyclerview.adapter = adapter
            recyclerview.setHasFixedSize(true)
    
        }
    
    
        override fun onItemClick(position: Int,adapter: CustomAdapter,v:View) {
            val clicked_item:UserModel = data[position]
            clicked_item.title = "clicked"
            clicked_item.isSelected = !clicked_item.isSelected
            if(clicked_item.isSelected){
                recycler_view.getChildAt(recycler_view.indexOfChild(v)).setBackgroundColor(Color.YELLOW)
                recycler_view.getChildAt(recycler_view.indexOfChild(v)).findViewById(R.id.linear_content).setBackgroundColor(Color.YELLOW)
            }else{
                recycler_view.getChildAt(recycler_view.indexOfChild(v)).setBackgroundColor(Color.WHITE)
                recycler_view.getChildAt(recycler_view.indexOfChild(v)).findViewById(R.id.linear_content).setBackgroundColor(Color.WHITE)
            }
    
        }
    
    }  
    

    I hope you find it useful

提交回复
热议问题