【问题标题】:Single Radio Button click in Android Recyclerview在 Android Recyclerview 中单击单个单选按钮
【发布时间】:2017-03-24 17:39:53
【问题描述】:

我正在使用 Recycler View 包含带有单选按钮的项目列表。当列表大小大于一时,可以在列表中选择单选按钮。我的主要问题是列表项是一个..单选按钮根本无法正常工作,请帮我解决这个问题....

这是我的代码

            RadioButton checked_rb = (RadioButton) v;
            if (lastCheckedRB != null) {
                lastCheckedRB.setChecked(false );

            }
            lastCheckedRB=checked_rb;

【问题讨论】:

  • 您可以检查适配器中项目的长度,而不是相应地设置单选按钮的可见性。
  • 我的情况是,如果未选中单选按钮 Some_value=0,否则选中单选按钮 some_value=1 就像那样......单项单选按钮的问题
  • 我在朋友的帮助下得到了解决方案,使用/创建界面来处理单选按钮的点击事件

标签: android android-recyclerview radio-button


【解决方案1】:

试试这个

public class RadioButtonAdapter extends RecyclerView.Adapter<RadioButtonAdapter.ViewHolder> {

private static final String TAG = RadioButtonAdapter.class.getSimpleName();
private List<String> tags;
private TagClickCallBack mTagClickCallBack;
private int lastCheckedPosition = -1;

public RadioButtonAdapter(TagClickCallBack tagClickCallBack) {
    tags = new ArrayList<>();
    this.mTagClickCallBack = tagClickCallBack;
}

public void addTags(List<String> newTags) {
    tags.addAll(newTags);
    notifyDataSetChanged();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.radiobtn_adapter, parent, false);
    return new ViewHolder(mView);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.rdoBtnProfession.setText(tags.get(position));
    holder.rdoBtnProfession.setChecked(position == lastCheckedPosition);
}

@Override
public int getItemCount() {
    return tags == null ? 0 : tags.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    @Bind(R.id.rdoBtnProfession)
    public AppCompatCheckBox rdoBtnProfession;

    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        rdoBtnProfession.setOnClickListener(v -> {
            lastCheckedPosition = getAdapterPosition();
            notifyItemRangeChanged(0, tags.size());
            mTagClickCallBack.onTagClicked(tags.get(getAdapterPosition()));
        });
    }
}

【讨论】:

    【解决方案2】:

    您可以通过设置单选/多选标志等模式来创建自定义视图。

    how to set choice mode single for listview with images

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-13
      • 2020-05-26
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 2017-09-08
      相关资源
      最近更新 更多