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

发布于 2025-01-10 06:38:29 字数 1883 浏览 2 评论 0原文

所以我知道如何从正常活动开始一项活动到另一项活动,但我不知道该怎么做。我对 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
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2025-01-17 06:38:29

首先,您需要将上下文作为参数传递到 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>

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

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文