gpt4 book ai didi

kotlin - RecyclerView onClick in Kotlin

转载 作者:行者123 更新时间:2023-12-02 13:36:35 39 4
gpt4 key购买 nike

我是新来的android学习者,我正在尝试制作 RecyclerView 包含(故事标题和故事图像)列表的列表。
当您在RecyclerView中单击某个项目时,它应该打开一个名为ChildrenStoriesPreview的新 Activity ,其中包含ScrollView,其中包含ImageView可以将Story Image放入其中,而TextView可以将Story Text放入其中。
问题是我不知道如何设置 ocItemClickListener 才能知道单击了哪个项目,以及新 Activity 如何包含取决于该项目的信息?请你帮助我好吗?

这是我的Main.kt

class MainChildrenStories : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_children_stories)

var childrenStoriesArraylist = ArrayList<ChildrenStoriesRecyclerView>()
childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story1", R.drawable.pic1))
childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story2", R.drawable.pic2))
childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story3", R.drawable.pic3))

children_stories_recyclerview.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)

val childrenStoriesAdapter = ChildrenStoriesAdapter(childrenStoriesArraylist)
children_stories_recyclerview.adapter = childrenStoriesAdapter

childrenStoriesAdapter.setOnItemClickListener(object : ChildrenStoriesAdapter.ClickListener {
override fun onClick(pos: Int, aView: View) {

//The App Crash here
if (pos == 0){
my_text_view.text = "Story number 1"
my_imageview.setImageResource(R.drawable.pic1)
}else if (pos == 1){
my_text_view.text = "Story number 2"
my_imageview.setImageResource(R.drawable.pic2)
}
val intent = Intent(this@MainChildrenStories, ChildrenStoryPreview::class.java)
startActivity(intent)
}
})

}
}

MyRecyclerView类
data class ChildrenStoriesRecyclerView(var mStoryName: String, var mStoryImage: Int)

我的RecyclerView适配器类
class ChildrenStoriesAdapter(var myArrayList: ArrayList<ChildrenStoriesRecyclerView>) :
RecyclerView.Adapter<ChildrenStoriesAdapter.ViewHolder>() {

lateinit var mClickListener: ClickListener

fun setOnItemClickListener(aClickListener: ClickListener) {
mClickListener = aClickListener
}

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
val v = LayoutInflater.from(p0.context).inflate(R.layout.children_stories_list, p0, false)
return ViewHolder(v)
}

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

override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
var infList = myArrayList[p1]
p0.storyName.text = infList.mStoryName
p0.storyImage.setImageResource(infList.mStoryImage)
}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
override fun onClick(v: View) {
mClickListener.onClick(adapterPosition, v)
}

val storyName = itemView.findViewById(R.id.txtStoryName) as TextView
val storyImage = itemView.findViewById(R.id.imageViewChildrenStories) as ImageView

init {
itemView.setOnClickListener(this)
}
}

interface ClickListener {
fun onClick(pos: Int, aView: View)
}
}

我的新 Activity 以显示故事的详细信息
class ChildrenStoryPreview : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_children_story_preview)

}
}

最佳答案

将事件侦听器传递给Adapter构造函数也传递给Viewholder来捕获View Holder(项目)单击。

class ChildrenStoriesAdapter(var myArrayList: ArrayList<ChildrenStoriesRecyclerView>
var clickListener:MyClickListener?) :
RecyclerView.Adapter<ChildrenStoriesAdapter.ViewHolder>() {
...
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
val v = LayoutInflater.from(p0.context).inflate(R.layout.children_stories_list, p0, false)
return ViewHolder(v, clickListener)
}
...

inner class ViewHolder(itemView: View, clickListener:MyClickListener?) :
RecyclerView.ViewHolder(itemView) {
itemView.setOnClickListener { clickListener?.myClickedFun(...) }
...

class ChildrenStoryPreview : AppCompatActivity(), MyClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_children_story_preview)
}
override fun myClickedFun(...) {
...
}
}

后来的初始化适配器像
..
 val childrenStoriesAdapter = ChildrenStoriesAdapter(childrenStoriesArraylist, this)

关于kotlin - RecyclerView onClick in Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55206664/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com