如何设置使用 Kotlin 检查的 RecyclerView 中的第一个单选按钮?

Hel*_*oCW 2 android kotlin android-recyclerview

我使用代码 A 创建一个带有单选按钮的 RecyclerView,该按钮基于从网站搜索到的一些示例代码。

\n\n

1\xe3\x80\x81不知道这些代码好不好,有没有更好的方法在RecyclerView中实现单选按钮?

\n\n

2\xe3\x80\x81如何设置启动APP时默认选中的第一个单选按钮?你知道,当我启动应用程序时,没有选中任何单选按钮。

\n\n

代码A

\n\n
class CustomAdapter (val backupItemList: List<MSetting>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {\n\n    private var mSelectedItem = -1\n\n    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {\n        val v = LayoutInflater.from(parent.context).inflate(R.layout.item_recyclerview, parent, false)\n        return ViewHolder(v)\n    }\n\n    fun getSelectedItem():Int{\n        return  mSelectedItem\n    }\n\n    override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {\n        holder.bindItems(backupItemList[position])\n        holder.itemView.radioButton.setChecked(position == mSelectedItem);\n    }\n\n    override fun getItemCount(): Int {\n        return backupItemList.size\n    }\n\n    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {\n        fun bindItems(aMSetting: MSetting) {\n           itemView.radioButton.tag=aMSetting._id\n            itemView.textViewUsername.text=aMSetting.createdDate.toString()\n            itemView.textViewAddress.text=aMSetting.description\n\n            itemView.radioButton.setOnClickListener {\n                mSelectedItem=getAdapterPosition()\n                notifyDataSetChanged();\n            }\n        }\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

XML文件

\n\n
<?xml version="1.0" encoding="utf-8"?>\n<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"\n    android:layout_width="match_parent"\n    android:layout_height="wrap_content"\n    android:orientation="vertical">\n\n\n    <android.support.v7.widget.CardView\n        android:layout_width="match_parent"\n        android:layout_height="wrap_content"\n        android:layout_margin="5dp">\n\n        <LinearLayout\n            android:layout_width="match_parent"\n            android:layout_height="wrap_content"\n            android:orientation="horizontal">\n\n\n               <RadioButton\n                android:id="@+id/radioButton"\n                android:layout_width="wrap_content"\n                android:layout_height="wrap_content"\n                android:text="My First" />\n\n            <TextView\n                android:id="@+id/textViewUsername"\n                android:layout_width="wrap_content"\n                android:layout_height="wrap_content"\n                android:padding="15dp"\n                android:text="Belal Khan"\n                />\n\n           <TextView\n                android:id="@+id/textViewAddress"\n                android:layout_width="match_parent"\n                android:layout_height="wrap_content"\n                android:padding="15dp"\n                android:text="Ranchi, Jharkhand"\n            />\n\n        </LinearLayout>\n\n</android.support.v7.widget.CardView>\n
Run Code Online (Sandbox Code Playgroud)\n

Sla*_*kov 6

    private var mSelectedItem = -1
Run Code Online (Sandbox Code Playgroud)

...

    override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
    holder.bindItems(backupItemList[position], position, mSelectedItem)
}
Run Code Online (Sandbox Code Playgroud)

...

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(aMSetting: MSetting, position: Int, selectedPosition: Int) {
    itemView.radioButton.tag=aMSetting._id
    itemView.textViewUsername.text=aMSetting.createdDate.toString()
    itemView.textViewAddress.text=aMSetting.description

    if ((selectedPosition == -1 && position == 0))
        itemView.radioButton.setChecked(true)
    else        
        if (selectedPosition == position)
            itemView.radioButton.setChecked(true)
        else
            itemView.radioButton.setChecked(false)


            itemView.radioButton.setOnClickListener {
                mSelectedItem=getAdapterPosition()
                notifyDataSetChanged()
            }
    }
Run Code Online (Sandbox Code Playgroud)

}