如何在Kotlin中定义一个返回RadioButton当前值的属性? [英] How can I define a property that returns the current value of a RadioButton in Kotlin?

查看:133
本文介绍了如何在Kotlin中定义一个返回RadioButton当前值的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码B是带有RadioButton的自定义RecyclerView.Adapter.

The Code B is a customized RecyclerView.Adapter with RadioButton.

我希望获得RadioButton的当前选定索引,因此我在代码A中添加了属性mySelectedIndex,但是mySelectedIndex在第一次调用后不会改变.

I hope to get the current selected index of the RadioButton, so I add a property mySelectedIndex in Code A, but mySelectedIndex will not change after it's called fro the first time.

我该怎么做?谢谢!

还有

private lateinit var selectedIndex= mCustomAdapter.getSelectedIndex() will not work too!

代码A

private lateinit var mCustomAdapter: CustomAdapter

private val mySelectedIndex by lazy {
        mCustomAdapter.getSelectedIndex()
}


private fun a(){
  backup(mySelectedIndex)
}


private fun b(){
  restore(mySelectedIndex) 
}

代码B

class CustomAdapter (val backupItemList: List<MSetting>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    val noRecord=-1
    private var mSelectedIndex = noRecord

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.item_recyclerview, parent, false)
        return ViewHolder(v)
    }

    fun getSelectedIndex():Int{
        return  mSelectedIndex
    }

    fun setSelectedIndex(index:Int){
        if (index in 0..(backupItemList.size-1) ){
            mSelectedIndex=index
        }
        notifyDataSetChanged();
    }

    override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
        holder.bindItems(backupItemList[position])
    }

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

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindItems(aMSetting: MSetting) {          

            itemView.radioButton.setOnClickListener {
                mSelectedIndex=adapterPosition
                notifyDataSetChanged();
            }

            if(adapterPosition == 0 && mSelectedIndex == noRecord) {            
                itemView.radioButton.isChecked = true
                mSelectedIndex=adapterPosition
            }
            else {
                itemView.radioButton.isChecked =(adapterPosition == mSelectedIndex)
            }
        }

    }

}

推荐答案

在此处使用by lazy委托,您可以确保在mySelectedIndex的首次初始化后将返回相同的值.

By using the by lazy delegation here you're making sure that the same value will be returned after the first initialization of mySelectedIndex.

您可能想要省略该代表团,而是执行以下操作:

You might want to omit the delegation and do something like this instead:

private val mySelectedIndex
    get () = mCustomAdapter.getSelectedIndex()

请注意,以上代码段不等于以下内容:

As a side note, this snippet above is not equal to the following:

private val mySelectedIndex
    get () = {
        mCustomAdapter.getSelectedIndex()
    }

后者将返回对getSelectedIndex()的函数引用,而前者将返回其结果.

The latter will return a function reference to getSelectedIndex() while the former will return its result.

这篇关于如何在Kotlin中定义一个返回RadioButton当前值的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆