I am making a grid layout with the help of Recyclerview and I have already added four Recyclerview items in a grid programmatically and now I want to navigate to different fragments when I click on different items. I am unable to find any appropriate way to do so.
            Asked
            
        
        
            Active
            
        
            Viewed 879 times
        
    0
            
            
        - 
                    so nothing here has helped ? https://stackoverflow.com/questions/24471109/recyclerview-onclick – a_local_nobody Mar 14 '22 at 06:53
3 Answers
1
            You should use if condition in onBindViewHolder of your adpter class as bellow:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.item.setOnClickListener {
        when (position) {
            0 -> {
                //navigate to first fragment}
            }
            1 -> {
                //navigate to second fragment}
            }
            2 -> {
                //navigate to third fragment}
            }
            3 -> {
                //navigate to fourth fragment}
            }
        }
    }
}
 
    
    
        Alireza Barakati
        
- 1,016
- 2
- 9
- 23
0
            
            
        Try below code
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.textViewName.text = seriesList[position].name
        holder.textViewNameDesc.text = seriesList[position].desc
        holder.textViewName.setOnClickListener {
                            Toast.makeText(context,"clicked",Toast.LENGTH_SHORT).show()
        }
    }
You need to add above code in your recycler adapter
 
    
    
        Sumit Kumar
        
- 263
- 1
- 6
0
            
            
        override fun onBindViewHolder(holder: StPanelHomeViewHolder, position: Int) {
        val item = dataset[position]
        holder.cardViewNavigation[0].setOnClickListener {
            val action =
                StPanelHomeFragmentDirections.actionStPanelHomeFragmentToStPanelPayFeeFragment()
            holder.view.findNavController().navigate(action)
        }
    }
 
    
    
        Harikant Sharma
        
- 43
- 1
- 5