RecyclerView onClick in Kotlin

时间:2019-03-17 11:43:49

标签: kotlin android-recyclerview

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

这是我的Main.kt

enter code here

MyRecyclerView类

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

   }
}

我的RecyclerView适配器类

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

我的新活动以显示故事的详细信息

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

1 个答案:

答案 0 :(得分:0)

还将事件侦听器传递给适配器构造函数,同时传递给视图持有者以捕获视图持有者(项目)的点击。

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)