我使用代码A创建一个带有单选按钮的RecyclerView,其中包含一些来自网站的搜索样本代码。
1,我不知道这些代码是否合适,是否有更好的方法在RecyclerView中实现单选按钮?
2,启动APP时如何设置默认的第一个单选按钮?您知道我启动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>
答案 0 :(得分:2)
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()
}
}
}
答案 1 :(得分:1)
如果要将列表中的第一个RadioButton
设置为true,可以检查adapterPosition,如果它是0(即第一项),则将其设置为true。
例如:
class CustomAdapter
{
var mSelectedItem = 0
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(aMSetting: MSetting) {
....
if(adapterPosition == mSelectedItem)
itemView.radioButton.checked = true
else
itemView.radioButton.checked = false
}
}
}