如何从内部 class 视图持有者启动 activity? [Android 工作室] [kotlin]

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

所以我知道如何从一个普通的 activity 开始一个 activity 到另一个,但我不知道该怎么做。我对 android 几乎一无所知,我的教授也没有真正解释任何事情,所以如果这个问题很难理解或其他什么,我深表歉意。请要求任何澄清。

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
    }
}

首先,您需要在 RecyclerAdapter 中将上下文作为参数传递。并且,接下来您需要做的是为 CardLayoutBinding 的根视图提供一个 ID。例如在这里我假设你的 CardLayoutBinding XML 看起来像这样。因此,我已将 id 提供给根视图 '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>

现在,在您的适配器中 class 进行以下更改:

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))
            }
        }
    }
}
.......
.......

}