Search code examples
android-studiokotlinandroid-activity

how to start an activity from inner class view holder? [Android studio] [kotlin]


So i know how to start an activity from an normal activity to another, but i do not know how to do this. I hardly know anything about android and my professor doesn't really explain anything so I apologize if this question is hard to understand or something. Please ask for any clarification.

package com.example.myrecyclerview

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.myrecyclerview.databinding.CardLayoutBinding

class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {

    private var titles = arrayOf("Little Gooby Jr", "Nathaniel Scuttlebug", "Indigo Purple", "Eathan Fishtic",
        "Malli mailman", "Cameron Silva", "Frozen Candy", "Gamer Jones", "Nuzz Lightyear", "Nueben Sandwich")
    private var image = arrayOf(R.drawable.goob, R.drawable.psy, R.drawable.indigo, R.drawable.teo,
        R.drawable.malli, R.drawable.cam, R.drawable.frozen, R.drawable.gamer, R.drawable.nazz, R.drawable.nueb)

    inner class ViewHolder(val binding: CardLayoutBinding): RecyclerView.ViewHolder(binding.root){
        init {
            itemView.setOnClickListener{
                val position: Int = bindingAdapterPosition

                //
                // code to start another activity and pass info like view holder position
                //
                
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.ViewHolder {
        val binding = CardLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
        with(holder){
            with(binding){
                itemImage.setImageResource(image[position])
                itemTitle.text = titles[position]
            }
        }
    }

    override fun getItemCount(): Int {
        return titles.size
    }
}

Solution

  • Firstly, you need to pass context as a parameter in your RecyclerAdapter. And, next thing you need to do is give an id to your root View of CardLayoutBinding. for e.g. here I supposed that your CardLayoutBinding XML looks something like this. So, I have provided id to the root view as 'itemViewContainer'

    card_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <data>
    
    </data>
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/itemViewContainer"
        android:layout_margin="10dp"
        >
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/itemImage"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/itemImage"
            android:id="@+id/itemTitle"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Now, in your adapter class you do the following changes:

    RecyclerAdapter.java

    class RecyclerAdapter(val context:Context) : RecyclerView.Adapter<...>(){
     .......
     .....
    
     inner class ViewHolder(val binding: ItemLayoutBinding): RecyclerView.ViewHolder(binding.root){}
      .......
      ......
    
     override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
        with(holder){
            with(binding){
                itemImage.setImageResource(image[position])
                itemTitle.text = titles[position]
                
                //from here you can click on item to start new Activity and pass position.you can also use intent to  pass position either.
                itemViewContainer.setOnClickListener {
                    val bundle = Bundle()
                    bundle.putInt("position", position)
                    context.startActivity(Intent(context, YourDesiredActivity::class.java).putExtras(bundle))
                }
            }
        }
    }
    .......
    .......
    
    }