简体   繁体   English

Recyclerview的Kotlin新片段

[英]Kotlin new Fragment from Recyclerview

I'm trying to open a new fragment from the current fragment's RecyclerView Adapter.But i don't know how to set onClickListener.. Here is my code 我正在尝试从当前片段的RecyclerView Adapter中打开一个新片段。但是我不知道如何设置onClickListener。这是我的代码

internal class ContentAdapter constructor (private var dataList: ArrayList<DataList>) : RecyclerView.Adapter<ContentAdapter.ViewHolder>(), View.OnClickListener {
    override fun onClick(v: View?) {
        val manager = activity.supportFragmentManager // I cannot resolve activity here 
        val transaction = manager.beginTransaction()
        val book = IndividualBook()
        transaction.add(android.R.id.content, book, "IndividualBook")
        transaction.addToBackStack(null)
        transaction.commit()
    }

    override fun getItemCount(): Int = dataList.size

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.item,parent,false))
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        holder!!.bind(dataList)
        holder.itemView.setOnClickListener(this)
    }

    class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        fun bind(DataList: ArrayList<DataList>){
            val dataList = DataList[adapterPosition]
            itemView.itemTitle.text = dataList.title
            Glide.with(itemView.context).load(dataList.image).into(itemView.itemImage)
            itemView.regPrice.text = dataList.regPrice
            itemView.regPrice.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
            itemView.splPrice.text = dataList.splPrice
        }
    }
}

Help me to solve the issue. 帮我解决问题。

It will be better to use callback to activity to navigate to new fragment. 最好使用回调活动来导航到新片段。

Change constructor to 将构造函数更改为

internal class ContentAdapter constructor (private var dataList: ArrayList<DataList>,private var callback:()->Unit) : RecyclerView.Adapter<ContentAdapter.ViewHolder>(), View.OnClickListener 

Then inside 然后里面

override fun onClick(v: View?)

put

callback.invoke();

Activity will set adapter by 活动将通过设置适配器

val adapter = ContentAdapter(datalist)
{ // Code to navigate to new fragment

}
internal class ContentAdapter (private val activity: Activity, private var dataList: ArrayList<DataList>) : RecyclerView.Adapter<ContentAdapter.ViewHolder>(), View.OnClickListener

You need to pass the activity in your constructor. 您需要在构造函数中传递活动。 And a nice bonus tip for kotlin: If you have only one primary constructor you don't have to write the "constructor" prefix 对于Kotlin来说,还有一个不错的奖励提示:如果只有一个主构造函数,则不必编写“ constructor”前缀

internal class ContentAdapter (private val activity: AppCompatActivity, private var dataList: ArrayList<DataList>) : RecyclerView.Adapter<ContentAdapter.ViewHolder>(), View.OnClickListener

When you have the activity you can access the manager 活动完成后,您可以访问管理员

You need to pass the activity in the constructor And a kotlin tip if you have only one primary constructor you don't have to write it. 您需要在构造函数中传递活动。如果只有一个主要构造函数,则不必编写Kotlin技巧。

A few solutions: 一些解决方案:

  1. Kotlin defaults all classes to static . Kotlin将所有类默认为static You have to opt-in to them being inner classes. 您必须选择加入他们作为inner类。 Put the class inside of an Activity, and add the inner modifier to the class: 将类放入Activity中,然后将inner修饰符添加到类中:

     internal inner class ContentAdapter constructor (private var dataList: ArrayList<DataList>) : RecyclerView.Adapter<ContentAdapter.ViewHolder>(), View.OnClickListener { 
  2. Keep the ContentAdapter static , but pass the activity in through the constructor: 保持ContentAdapter static ,但将activity通过构造函数传递:

     internal class ContentAdapter( private val activity: AppCompatActivity, private var dataList: ArrayList<DataList> ) : RecyclerView.Adapter<ContentAdapter.ViewHolder>(), View.OnClickListener { 

It looks like you probably aren't familiar with the ways that Kotlin differs from Java and defaults you to writing better code. 看来您可能不熟悉Kotlin与Java的不同,并且默认将您编写更好的代码。 It might help for you to read through the Kotlin documentation in entirety. 它可能会帮助您完整阅读Kotlin文档。 It's not very long. 时间不长 I also recommend Kotlin Koans which are coding exercises JetBrains provides to practice Kotlin. 我还推荐Kotlin Koans,这是JetBrains提供的用于练习Kotlin的编码练习。

Kotlin is a huge improvement over Java :-) Don't let this discourage you! Kotlin是对Java的巨大改进:-)不要让这种情况阻止您!

(By the way, it looks like you can probably make your dataList a val . val is always preferred over var when the reference is final .) (顺便说一句,您似乎可以将dataListval 。当引用为final时,始终首选val而不是var 。)

Use Onclick as alternative of onClickListener, you can use onClick like this. 使用Onclick作为onClickListener的替代方法,您可以像这样使用onClick。

 itemView.regPrice.onClick {
/ * your action */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM