Home > Enterprise >  How to set OnclickListener in recyclerView for going another activity
How to set OnclickListener in recyclerView for going another activity

Time:08-17

**Hello everyone. I am new in android development. I am using room database and I set data in recyclerView. Now I am trying to Do when I click any recyclerview item then I need to go another activity.Anyone Please help me. **

MainActivity RecyclerView Adding code

 private fun setUpListOfDataIntoRecyclerView(registerList:ArrayList<registerEntity>,
                                                registerDao: registerDao){

        if(registerList.isNotEmpty()){
            var itemAdapter = MainAdapter(registerList)
            
            binding?.rvHappyPlaceList?.layoutManager = LinearLayoutManager(this)
            binding?.rvHappyPlaceList?.adapter = itemAdapter
            binding?.rvHappyPlaceList?.visibility = View.VISIBLE
            binding?.noRecordText?.visibility = View.GONE
        }else{
            binding?.rvHappyPlaceList?.visibility = View.GONE
            binding?.noRecordText?.visibility = View.VISIBLE
        }
    }

MainAdapter

class MainAdapter(val items: ArrayList<registerEntity>):
    RecyclerView.Adapter<MainAdapter.MainHolder>() {

    //step 2
    private var onClickListener: AdapterView.OnItemClickListener? = null

    inner class MainHolder(var binding: ItemHappyPlaceBinding): RecyclerView.
                           ViewHolder(binding.root) {
                                val llTitle = binding.tvTitle
                                 val llDescription = binding.tvDescription
                                  var llImage = binding.ivPlaceImage
                         }

    fun setOnClickListener(onClickListener: View.OnContextClickListener){
        this.onClickListener = onClickListener
    }



    override fun onBindViewHolder(holder: MainHolder, position: Int) {
        val item = items[position]
        holder.llTitle.text = item.title
        holder.llDescription.text = item.description
        holder.llImage.setImageURI(Uri.parse(item.image))
    }

CodePudding user response:

I recommend you replace:

private var onClickListener: AdapterView.OnItemClickListener? = null

with this:

var onItemClick: (() -> Unit)? = null

You can also delete the fun setOnClickListener(..){}

And add in your MainHolder the binding.root.setOnClickListener{} like:

inner class MainHolder(var binding: ItemHappyPlaceBinding): RecyclerView.ViewHolder(binding.root) {
  init {
    binding.root.setOnClickListener {
      onItemClick?.invoke()
    }
  }
  
  val llTitle = binding.tvTitle
  val llDescription = binding.tvDescription
  var llImage = binding.ivPlaceImage
}

Or if you prefer u can also add the onItemClick?.invoke() in the onBindViewHolder like:

  holder.itemView.setOnClickListener {
    onItemClick?.invoke()
  }

And then in your activity, in fun setUpListOfDataIntoRecyclerView add:

var itemAdapter = MainAdapter(registerList)
itemAdapter.onItemClick = this::goToActivity() 

And also add the goToActivity fun like:

fun goToActivity(){
  // launch the activity
}
  • Related