title: Implement Click Listener On RecyclerView Item android
body: <p>I have wrote the following code for recyclerview of my project. I want to implement clicklistener on an item of this recyclerview. Can you guide me to find the problem in my code?</p>
  <blockquote>
  <p>fragment_home.xml</p>
  </blockquote>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:background=&quot;@color/colorWhite&quot;
      tools:context=&quot;.view.ui.HomeFragment&quot;&gt;
  
      &lt;include
          android:id=&quot;@+id/toolbarHome&quot;
          layout=&quot;@layout/layout_toolbar&quot; /&gt;
  
      &lt;androidx.appcompat.widget.SearchView
          android:id=&quot;@+id/svSearchCountry&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;@dimen/_15sdp&quot;
          android:layout_marginTop=&quot;@dimen/_15sdp&quot;
          android:layout_marginEnd=&quot;@dimen/_15sdp&quot;
          android:background=&quot;@drawable/layout_rectangle_edittext&quot;
          android:textCursorDrawable=&quot;@null&quot;
          app:iconifiedByDefault=&quot;false&quot;
          app:layout_constraintTop_toBottomOf=&quot;@id/toolbarHome&quot;
          app:queryBackground=&quot;@null&quot;
          app:queryHint=&quot;Search Country&quot; /&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id=&quot;@+id/rvCountries&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;0dp&quot;
          android:layout_marginStart=&quot;@dimen/_5sdp&quot;
          android:layout_marginTop=&quot;@dimen/_15sdp&quot;
          android:layout_marginEnd=&quot;@dimen/_5sdp&quot;
          android:layout_marginBottom=&quot;@dimen/_5sdp&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@id/svSearchCountry&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <blockquote>
  <p>layout_country_item.xml</p>
  </blockquote>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:layout_width=&quot;match_parent&quot;
      android:id=&quot;@+id/countryItemParent&quot;
      android:layout_height=&quot;wrap_content&quot;&gt;
  
      &lt;ImageView
          android:id=&quot;@+id/ivCountry&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;0dp&quot;
          android:padding=&quot;@dimen/_5sdp&quot;
          android:layout_marginTop=&quot;@dimen/_5sdp&quot;
          android:src=&quot;@drawable/kenya&quot;
          app:layout_constraintDimensionRatio=&quot;1:1&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/tvCountryName&quot;
          style=&quot;@style/CustomTextViewMedium&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Kenya&quot;
          android:textColor=&quot;@color/colorText&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@id/ivCountry&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <blockquote>
  <p>HomeFragment.kt</p>
  </blockquote>
  <pre><code>class HomeFragment : Fragment() {
      private lateinit var listener: CountriesAdapter.CountriesAdapterListener
      private lateinit var countriesAdapter: CountriesAdapter
      lateinit var toolbar: Toolbar
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          return inflater.inflate(R.layout.fragment_home, container, false)
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          toolbar = view.findViewById(R.id.toolbar) as Toolbar
          setupUI()
          setupListeners()
          setupRecyclerView()
  
      }
  
      private fun setupListeners() {
          listener = object : CountriesAdapter.CountriesAdapterListener {
              override fun onClickedItem() {
                  Log.d(&quot;onItemClickedCalled&quot;,&quot;Working&quot;)
                  startActivity(Intent(requireActivity(), HomeActivity::class.java))
  
              }
          }
  
          svSearchCountry.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
              override fun onQueryTextSubmit(query: String?): Boolean {
                  return false
              }
  
              override fun onQueryTextChange(newText: String?): Boolean {
                  countriesAdapter.filter.filter(newText)
                  return false
              }
          })
      }
  
      private fun setupRecyclerView() {
          countriesAdapter =
              CountriesAdapter(
                  listener,
                  CountriesModel.countriesList as ArrayList&lt;CountriesModel&gt;
              )
  
          rvCountries.apply {
              layoutManager = GridLayoutManager(context, 4, GridLayoutManager.VERTICAL, false)
              adapter = countriesAdapter
          }
      }
  
      private fun setupUI() {
          toolbar.title = &quot;Home&quot;
          val searchIcon = svSearchCountry.findViewById&lt;ImageView&gt;(R.id.search_mag_icon)
          searchIcon.setColorFilter(R.color.colorPrimary)
          val cancelIcon = svSearchCountry.findViewById&lt;ImageView&gt;(R.id.search_close_btn)
          cancelIcon.setColorFilter(R.color.colorText)
          val textView = svSearchCountry.findViewById&lt;TextView&gt;(R.id.search_src_text)
          textView.setTextColor(ContextCompat.getColor(activity as MainActivity, R.color.colorText))
      }
  }
  </code></pre>
  <blockquote>
  <p>CountriesAdapter.kt</p>
  </blockquote>
  <pre><code>class CountriesAdapter(
      private val listener: CountriesAdapterListener, var countriesList: ArrayList&lt;CountriesModel&gt;
  ) : RecyclerView.Adapter&lt;CountriesAdapter.CountriesViewHolder&gt;(), Filterable {
  
      var countryFilterList = ArrayList&lt;CountriesModel&gt;()
      init {
          countryFilterList = countriesList
      }
  
      interface CountriesAdapterListener {
          fun onClickedItem()
      }
  
      inner class CountriesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
          val ivCountryImage: ImageView = itemView.findViewById(R.id.ivCountry)
          val tvCountryTitle: TextView = itemView.findViewById(R.id.tvCountryName)
          val countryItemParent: ConstraintLayout = itemView.findViewById(R.id.countryItemParent)
      }
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountriesViewHolder {
          val view = LayoutInflater.from(parent.context)
              .inflate(R.layout.layout_country_item, parent, false)
          return CountriesViewHolder(view)
      }
  
      override fun onBindViewHolder(holder: CountriesViewHolder, position: Int) {
          val countryItem = countryFilterList[position]
          Glide.with(holder.ivCountryImage.context).load(countryItem.imageURI)
              .into(holder.ivCountryImage)
          holder.tvCountryTitle.text = countryItem.title
          if (position%4==0){
              holder.itemView.visibility = View.INVISIBLE
          }
          holder.countryItemParent.setOnClickListener {
              listener.onClickedItem()
          }
      }
  
      override fun getItemCount(): Int {
          return countryFilterList.size
      }
  
      override fun getFilter(): Filter {
          return object : Filter() {
              override fun performFiltering(constraint: CharSequence?): FilterResults {
                  val charSearch = constraint.toString()
                  countryFilterList = if (charSearch.isEmpty()) {
                      countriesList
                  } else {
                      val resultList = ArrayList&lt;CountriesModel&gt;()
                      for (row in countriesList) {
                          if (row.title.toLowerCase(Locale.ROOT)
                                  .contains(charSearch.toLowerCase(Locale.ROOT))
                          ) {
                              resultList.add(row)
                          }
                      }
                      resultList
                  }
                  val filterResults = FilterResults()
                  filterResults.values = countryFilterList
                  return filterResults
              }
  
              @Suppress(&quot;UNCHECKED_CAST&quot;)
              override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
                  countryFilterList = results?.values as ArrayList&lt;CountriesModel&gt;
                  notifyDataSetChanged()
              }
  
          }
      }
  }
  </code></pre>
  <p>please help me out i got stucked here and had no idea how to solve this issue.</p>