第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

RadioGroup 中的 RecyclerView 和 RadioButton

RadioGroup 中的 RecyclerView 和 RadioButton

胡子哥哥 2021-09-12 20:39:25
我有 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>
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻1818條經(jīng)驗 獲得超11個贊

您必須將選定的單選按鈕 ID 保留到您的模型中。


1> 在你的模型中選擇 selectedId。


class Model{

  int selctedId;

  // getter setter

}

2> 將此 ID 附加到您的無線電組。


@Override

public void onBindViewHolder(final CoachListViewHolder holder, final int position) {

    Model model = list.get(position);

    holder.radioGroup.check(model.getSelectedId);

    holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()

    {

        public void onCheckedChanged(RadioGroup group, int checkedId)

        {

         model.setSelectedId(checkedId);

        }

    });

在這個解決方案中,我們在模型中保存了選定的 id,我們將此字段附加到無線電組中 radioGroup.check(model.getSelectedId);


原因

當(dāng)您不保留所選值時,它會在用戶滾動位置時被回收。


我也發(fā)現(xiàn)了一個相關(guān)的問題。


久經(jīng)考驗的解決方案

您正在使用數(shù)據(jù)綁定,因此上述解決方案可以更短。使用雙向綁定來保存選定的 id。


項目.java


public class Item extends BaseObservable{

    private int selectedId;


    public int getSelectedId() {

        return selectedId;

    }


    public void setSelectedId(int selectedId) {

        this.selectedId = selectedId;

    }

}

行列表.xml


<data>


    <variable

        name="item"

        type="com.innovanathinklabs.sample.ui2.Item"/>


</data>


<RadioGroup

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginBottom="20dp"

    android:background="@color/colorPrimary"

    android:checkedButton="@={item.selectedId}"

    >


    <android.support.v7.widget.AppCompatRadioButton

        android:id="@+id/rbMale"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="male"/>


    <android.support.v7.widget.AppCompatRadioButton

        android:id="@+id/rbFemale"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="female"/>

</RadioGroup>

現(xiàn)在,當(dāng)您需要獲取所選項目時。然后這樣做。


if(list.get(1).getSelectedId() == R.id.rbMale){

 // male is selected

}

else if (list.get(1).getSelectedId() == R.id.rbMale){

 // female is selcted

}

同時從 Radio 組和 Radio 按鈕中刪除任何其他不必要的邏輯。


數(shù)據(jù)綁定魔法是

此代碼轉(zhuǎn)換為 android:checkedButton="@={item.selectedId}"


查看完整回答
反對 回復(fù) 2021-09-12
?
寶慕林4294392

TA貢獻2021條經(jīng)驗 獲得超8個贊

添加private int isFirstQuestionChecked = false到您的模型并在您選擇此項時對其進行更改RadioButton。在您的適配器中顯示正確的值RadioButton


if (element.isFirstQuestionChecked == true) {

    selectRadioButton()

} else {

    deselectRadioButton() // it's important!

}


查看完整回答
反對 回復(fù) 2021-09-12
  • 2 回答
  • 0 關(guān)注
  • 329 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號