Kotlin-如何在RecyclerViewAdapter中使用Binding(非上下文)调用finish()

zi8p0yeb  于 2023-05-01  发布在  Kotlin
关注(0)|答案(2)|浏览(111)

我需要完成()点击项目/行时RecyclerView所在的Activity。
我查了一下,但大多数答案都提到了使用context(context as MyActivity).finish()时的旧方法。如何使用binding执行相同的操作

class MyAdapter(var users: List<User>): RecyclerView.Adapter<MyAdapter.UserViewHolder>() {

    inner class UserViewHolder(private val binding: UserRowBinding): RecyclerView.ViewHolder(binding.root) {

        init {
            binding.root.setOnClickListener {

                val intent = Intent(binding.root.context, NextActivity::class.java)
                binding.root.context.startActivity(intent)

                // call finish() here
            }
        }
    }
}

class MyActivity: App: AppCompatActivity() {

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // ...

        val adapter = MyAdapter(some_array_Of_users)
        binding.recyclerView.adapter = adapter
    }
}
8ehkhllq

8ehkhllq1#

finish()Activity类的方法。所以你的任务可以换个说法
如何从我的Adapter中获取Activity上的参考?
你至少有两个选择:
1.使用itemView获取Context

(itemView.context as Activity).finish()

1.从绑定中获取Context

(binding.root.context as Activity).finish()

小建议:

您还可以考虑将单击处理委托给调用方并将其传递给适配器。从架构上来说可能更正确。而且找到Activity的引用也不会有问题。就像这样:

class MyAdapter(var users: List<User>, private val onClick: OnClickListener): RecyclerView.Adapter<MyAdapter.UserViewHolder>() {

    inner class UserViewHolder(private val binding: UserRowBinding): RecyclerView.ViewHolder(binding.root) {

        init {
            binding.root.setOnClickListener {
                onClick()
            }
        }
    }
}

class MyActivity: App: AppCompatActivity() {

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // ...

        val adapter = MyAdapter(some_array_Of_users) {                     
            val intent = Intent(binding.root.context, NextActivity::class.java)
            startActivity(intent)
            this@MainActivity.finish()
        }
        binding.recyclerView.adapter = adapter
    }
}
sd2nnvve

sd2nnvve2#

您还可以在Activity本身上创建侦听器函数。
在Adapter中,您可以为侦听器添加一个变量。(或者你可以添加这个作为适配器的参数)

val listener: (()->Unit)? = null

在ViewHolder中,添加listener作为参数,并在setOnClickListener中调用它,它应该看起来像这样:

inner class UserViewHolder(private val binding: UserRowBinding, private val listener: (()->Unit)? = null): RecyclerView.ViewHolder(binding.root) {

    init {
        binding.root.setOnClickListener {
            listener?.invoke()
        }
    }
}

在Activity中,您可以只设置变量listener值。在这种情况下,您可以设置要查找的'finish()'。像这样的东西,

class MyActivity: App: AppCompatActivity() {

lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // ...

    val adapter = MyAdapter(some_array_Of_users)
    adapter.listener = { moveToOtherActivity() }
    this.adapter = adapter
}

  private fun moveToOtherActivity() {
      val intent = Intent(this@MainActivity, NextActivity::class.java)
      startActivity(intent)
      finish()
    }
}

或者只是在适配器中添加一个参数,

class MyAdapter(var users: List<User>, val listener: (()->Unit?=null): RecyclerView.Adapter<MyAdapter.UserViewHolder>()

在活动中,

val adapter = MyAdapter(some_array_Of_users, {moveToOtherActivity()})

希望这能帮上忙。

相关问题