【发布时间】:2019-05-28 12:48:15
【问题描述】:
我有一个RadioGroup,在RecyclerView 中有4 个RadioButtons。
RecyclerView 适配器是这样的
class QuestionRecyclerAdapter(var number:Int, val context: Context, val quesions: List<Question>,val qnADao: QnADao) : RecyclerView.Adapter<QuestionRecyclerAdapter.ViewHolder>() {
lateinit var question:Question
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.question_single_item, parent, false));
}
override fun getItemCount(): Int {
return quesions.size;
}
fun updateNumber(number: Int){
this.number = number
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
question = quesions[position]
holder.textTestQuestion.text = ""+(number+position)+" "+ question.question;
holder.answerFirst.text = "a. "+ question.answers?.get(0).toString()
holder.answerSecond.text = "b. "+ question.answers?.get(1).toString()
holder.answerThird.text = "c. "+ question.answers?.get(2).toString()
holder.answerFourth.text = "d. "+ question.answers?.get(3).toString()
holder.radio_group.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener{
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
when(checkedId){
R.id.answerFirst -> {
question.rightAnswer=0
}
R.id.answerSecond -> {
question.rightAnswer=1
}
R.id.answerThird -> {
question.rightAnswer=2
}
R.id.answerFourth -> {
question.rightAnswer=3
}
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textTestQuestion = view.test_text_question
val answerFirst = view.answerFirst
val answerSecond = view.answerSecond
val answerThird = view.answerThird
val answerFourth = view.answerFourth
val radio_group = view.radioGrp
}
}
question_single_item.xml是这样的
<RadioButton
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/fourth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
</RadioGroup>
</RelativeLayout>
在 recyclerview 中,我填充了近 100 条数据,因此有 100 个单选组和 100*4 单选按钮。所以我需要找出是否选择了同一个单选组中的相同单选按钮。换句话说,我需要确定选择的单选按钮(一组中的 4 个)是否来自同一组。任何帮助,将不胜感激。谢谢
【问题讨论】:
-
尝试使用类似于键值对的Hashmap,它将帮助您将每个问题设置为键
-
您想知道radioGroup 中的哪个radioButton 被选中了吗?
标签: android kotlin android-recyclerview radio-button radio-group