title: How to use androidx.recyclerview.selection in androidx.recyclerview.widget. Or how to select an item in recyclerview using kotlin in android?
body: <p>I have created a horizontal recycler view with items loaded. After that, I need to select that item and perform the click event. Up to this Archived(code is available in the following ), Now I want to change the color of the clicked item and remaining item should be unselected. Please apologize If I didn't query properly. </p>
  
  <p>This all thing built using Kotlin's Androidx Recyclerview. Here is <a href="https://github.com/googlesamples/android-RecyclerView/tree/master/kotlinApp/#readme" rel="nofollow noreferrer">sample code</a>! I'm finding a solution to the extension of this for selecting the item and changing its color.</p>
  
  <p>Android Activity Coded in kotlin</p>
  
  <pre><code>
  
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import android.view.View
  import android.view.WindowManager
  import androidx.recyclerview.widget.LinearLayoutManager
  import kotlinx.android.synthetic.main.activity_sample.*
  import androidx.recyclerview.widget.RecyclerView
  
  
  
  class SampleActivity : AppCompatActivity() {
  
      val bottle_name: ArrayList&lt;String&gt; = ArrayList()
      val bottle_img: ArrayList&lt;Int&gt; = ArrayList()
      val bottle_type: ArrayList&lt;String&gt; = ArrayList()
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_sample)
  
          addBottles()
  
  
          val adptr = BottlesAdaptor(bottle_name, bottle_img, bottle_type);
          bottles_list.adapter = adptr
  
          val layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
          val recyclerView = bottles_list
          recyclerView.setLayoutManager(layoutManager)
  
      }
  
      override fun onWindowFocusChanged(hasFocus: Boolean) {
          super.onWindowFocusChanged(hasFocus)
          if (hasFocus) {
              hideSystemUI()
          }
      }
  
      private fun hideSystemUI() {
          val decorView = window.decorView
          decorView.systemUiVisibility = (
                  View.SYSTEM_UI_FLAG_IMMERSIVE
                          or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                          or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                          or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                          or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                          or View.SYSTEM_UI_FLAG_FULLSCREEN
                  )
          window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  
      }
  
      fun addBottles(){
          bottle_name.add("Coca1");
          bottle_name.add("Coca2");
          bottle_name.add("Coca3");
          bottle_name.add("Coca4");
          bottle_name.add("Coca5");
          bottle_name.add("Coca6");
          bottle_name.add("Coca7");
          bottle_name.add("Coca8");
          bottle_type.add("cooldrink");
          bottle_type.add("cooldrink");
          bottle_type.add("cooldrink");
          bottle_type.add("cooldrink");
          bottle_type.add("cooldrink");
          bottle_type.add("wine");
          bottle_type.add("wine");
          bottle_type.add("wine");
          bottle_img.add(R.drawable.coca1);
          bottle_img.add(R.drawable.coca2);
          bottle_img.add(R.drawable.coca3);
          bottle_img.add(R.drawable.coca4);
          bottle_img.add(R.drawable.coca5);
          bottle_img.add(R.drawable.coca6);
          bottle_img.add(R.drawable.coca7);
          bottle_img.add(R.drawable.coca8);
  
  
      }
  
  </code></pre>
  
  <p>Bottles Adaptor coded using Kotlin</p>
  
  <pre><code>
  import android.content.Context
  import android.util.Log
  import android.view.LayoutInflater
  import android.view.ViewGroup
  import android.view.View
  import android.widget.ArrayAdapter
  import android.widget.ImageView
  import android.widget.TextView
  import androidx.recyclerview.widget.LinearLayoutManager
  import kotlinx.android.synthetic.main.bottle_list_item.view.*
  import androidx.recyclerview.widget.RecyclerView
  
  
  class BottlesAdaptor(private val dataSet: ArrayList&lt;String&gt;, private val dataSet2: ArrayList&lt;Int&gt;, private val dataSet3: ArrayList&lt;String&gt;) :
      RecyclerView.Adapter&lt;BottlesAdaptor.ViewHolder&gt;() {
  
      /**
       * Provide a reference to the type of views that you are using (custom ViewHolder)
       */
      class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
          val textView: TextView
          val imageView: ImageView
          val textView2:TextView
  
          init {
              // Define click listener for the ViewHolder's View.
              v.setOnClickListener {
                  Log.d(TAG, "Element $adapterPosition clicked.")
  // I have tried this but it is not giving me the opportunity to select a color for the selected item 
                  if (v.id==adapterPosition){
                      v.setBackgroundResource(R.drawable.bottle_card_selected);
                  }
                  else{
                      v.setBackgroundResource(R.drawable.bottle_card);
                  }
              }
              textView = v.findViewById(R.id.bottle_list_text)
              imageView = v.findViewById(R.id.bottle_list_img)
              textView2 = v.findViewById(R.id.bottle_list_subtext)
          }
      }
  
      // Create new views (invoked by the layout manager)
      override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
          // Create a new view.
          val v = LayoutInflater.from(viewGroup.context)
              .inflate(R.layout.bottle_list_item, viewGroup, false)
  
          return ViewHolder(v)
      }
  
      // Replace the contents of a view (invoked by the layout manager)
      override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
          Log.d(TAG, "Element $position set.")
  
          // Get element from your dataset at this position and replace the contents of the view
          // with that element
          viewHolder.textView.text = dataSet[position]
          viewHolder.textView2.text = dataSet3[position]
          viewHolder.imageView.setImageResource(dataSet2[position])
      }
  
      // Return the size of your dataset (invoked by the layout manager)
      override fun getItemCount() = dataSet.size
  
  
      companion object {
          private val TAG = "CustomAdapter"
      }
  }
  
  
  </code></pre>
  
  <p>I am expecting a better way of solving solution. also expecting to disable the particular button if possible. It should give the option to the selectable item of recyclerview</p>