如何从内部类视图持有者启动活动? [Android 工作室] [kotlin]
所以我知道如何从正常活动开始一项活动到另一项活动,但我不知道该怎么做。我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要将上下文作为参数传递到 RecyclerAdapter 中。接下来您需要做的就是为 CardLayoutBinding 的根视图提供一个 id。例如,我认为您的 CardLayoutBinding XML 看起来像这样。因此,我已将 id 提供给根视图作为“itemViewContainer”
现在,在您的适配器类中进行以下更改:
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'
Now, in your adapter class you do the following changes: