gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:17:02 27 4
gpt4 key购买 nike

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

1、不知道这些代码好不好,RecyclerView的单选按钮有没有更好的实现方式?

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

代码A

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

private var mSelectedItem = -1

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 getSelectedItem():Int{
return mSelectedItem
}

override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(backupItemList[position])
holder.itemView.radioButton.setChecked(position == mSelectedItem);
}

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

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

itemView.radioButton.setOnClickListener {
mSelectedItem=getAdapterPosition()
notifyDataSetChanged();
}
}
}

}

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">


<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My First" />

<TextView
android:id="@+id/textViewUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Belal Khan"
/>

<TextView
android:id="@+id/textViewAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Ranchi, Jharkhand"
/>

</LinearLayout>

</android.support.v7.widget.CardView>

最佳答案

    private var mSelectedItem = -1

...

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

...

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

关于android - 如何设置使用 Kotlin 检查的 RecyclerView 中的第一个单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49401240/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com