单一单选按钮单击Android Recyclerview

时间:2017-03-24 07:05:17

标签: android android-recyclerview radio-button

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

这是我的代码

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

            }
            lastCheckedRB=checked_rb;

2 个答案:

答案 0 :(得分:0)

试试这个

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()));
        });
    }
}

答案 1 :(得分:0)

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

how to set choice mode single for listview with images