RadioGroup 中的 RecyclerView 和 RadioButton

我有 RecyclerView,在 RowItem 布局中,我有 RadioGroup,其中有两个 RadioButton。如果我选择 First Item 并滚动到底部,第 8 个,第 16 个,...所以检查:| 怎么处理?


tnx的答案..如果你需要任何信息..我把主题


ques_item_row.xml


 <RadioGroup

                    android:id="@+id/ques_radio_group"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:layout_below="@+id/txt_ques_title"

                    android:layout_gravity="end|center"

                    android:gravity="start"

                    android:orientation="vertical"

                    android:textAlignment="textStart"

                    android:textDirection="rtl">


                    <android.support.v7.widget.AppCompatRadioButton

                        android:id="@+id/first_ques"

                        android:layout_width="match_parent"

                        android:layout_height="wrap_content"

                        android:layout_gravity="end|right"

                        android:layout_margin="@dimen/quest_margin"

                        android:gravity="end|right"

                        android:checked="false"

                        android:padding="@dimen/quest_padding"

                        android:text="@{question.firstQuest}"

                        android:textColor="@color/ques"

                        android:textDirection="ltr"

                        android:textSize="@dimen/text_ques"

                        />             

                </RadioGroup>


胡子哥哥
浏览 308回答 2
2回答

慕尼黑8549860

您必须将选定的单选按钮 ID 保留到您的模型中。1> 在你的模型中选择 selectedId。class Model{&nbsp; int selctedId;&nbsp; // getter setter}2> 将此 ID 附加到您的无线电组。@Overridepublic void onBindViewHolder(final CoachListViewHolder holder, final int position) {&nbsp; &nbsp; Model model = list.get(position);&nbsp; &nbsp; holder.radioGroup.check(model.getSelectedId);&nbsp; &nbsp; holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public void onCheckedChanged(RadioGroup group, int checkedId)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.setSelectedId(checkedId);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });在这个解决方案中,我们在模型中保存了选定的 id,我们将此字段附加到无线电组中 radioGroup.check(model.getSelectedId);原因当您不保留所选值时,它会在用户滚动位置时被回收。我也发现了一个相关的问题。久经考验的解决方案您正在使用数据绑定,因此上述解决方案可以更短。使用双向绑定来保存选定的 id。项目.javapublic class Item extends BaseObservable{&nbsp; &nbsp; private int selectedId;&nbsp; &nbsp; public int getSelectedId() {&nbsp; &nbsp; &nbsp; &nbsp; return selectedId;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSelectedId(int selectedId) {&nbsp; &nbsp; &nbsp; &nbsp; this.selectedId = selectedId;&nbsp; &nbsp; }}行列表.xml<data>&nbsp; &nbsp; <variable&nbsp; &nbsp; &nbsp; &nbsp; name="item"&nbsp; &nbsp; &nbsp; &nbsp; type="com.innovanathinklabs.sample.ui2.Item"/></data><RadioGroup&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginBottom="20dp"&nbsp; &nbsp; android:background="@color/colorPrimary"&nbsp; &nbsp; android:checkedButton="@={item.selectedId}"&nbsp; &nbsp; >&nbsp; &nbsp; <android.support.v7.widget.AppCompatRadioButton&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/rbMale"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="male"/>&nbsp; &nbsp; <android.support.v7.widget.AppCompatRadioButton&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/rbFemale"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="female"/></RadioGroup>现在,当您需要获取所选项目时。然后这样做。if(list.get(1).getSelectedId() == R.id.rbMale){&nbsp;// male is selected}else if (list.get(1).getSelectedId() == R.id.rbMale){&nbsp;// female is selcted}同时从 Radio 组和 Radio 按钮中删除任何其他不必要的逻辑。数据绑定魔法是此代码转换为 android:checkedButton="@={item.selectedId}"

宝慕林4294392

添加private int isFirstQuestionChecked = false到您的模型并在您选择此项时对其进行更改RadioButton。在您的适配器中显示正确的值RadioButtonif (element.isFirstQuestionChecked == true) {&nbsp; &nbsp; selectRadioButton()} else {&nbsp; &nbsp; deselectRadioButton() // it's important!}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java