点击RadioButton" A"切换RadioButton" B" RecyclerView

时间:2017-12-01 16:40:17

标签: java android android-recyclerview radio-button recycler-adapter

我有一个RecyclerView,显示国家/地区的RadioButtons列表,让用户选择他的国家/地区。我不想要点击项目,所以我已经在适配器的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);
                    }
                };
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

您应该将您的单选按钮放在一个广播组中。这样,其中只有一个被选中。我不知道为什么使用runnable来设置单选按钮!我希望这会对你有所帮助。

相关问题