簡體   English   中英

單擊RadioButton“ A”切換RadioButton“ B” RecyclerView

[英]Clicking RadioButton “A” toggles RadioButton “B” RecyclerView

我有一個RecyclerView,它顯示國家/地區的RadioButtons列表,以允許用戶選擇他的國家/地區。 我只希望單擊Item,所以我已經在適配器的onBindView方法中編寫了此OnCheckListener,但是每當我單擊RadioButton時,其他幾個按鈕就會隨之切換。

    @Override
    public void onBindViewHolder(final com.example.ameer.ta7adialma3rifa.adapters.CountriesAdapter.ViewHolder holder, final int position) {
        String name = mData.get(position).getName();
        int imgId = mData.get(position).getImageId();
        holder.radioButton.setText(name);
        holder.flagImageView.setImageResource(imgId);
        holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                RadioButton button = (RadioButton) compoundButton;
                if (!button.getText().equals(mData.get(position).getName())){
                    button.setChecked(false);
                }
                if (runnable != null){
                    runnable.run();
                }
                runnable = new Runnable() {
                    @Override
                    public void run() {
                        holder.radioButton.setChecked(false);
                    }
                };
            }
        });
    }

您應該將單選按鈕放在單選組中。 這樣,只選擇其中之一。 而且我不知道為什么要使用runnable將單選按鈕設置為選中狀態! 我希望這可以幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM