如何在每个广播按钮可以一起交互的每个项目中使用一个无线电按钮创建Android Recyclerview



当我检查收音机按钮时,我无法取消选中其他收回器视图中的其他单选按钮

我无法在广播按钮上达到其他项目视图位置

当我试图取消选中其他广播按钮时,所有广播按钮都没有选中..

覆盖娱乐onbindviewholder(持有人:搜索卡维特持有人,位置:int({ holder.bind(list [位置](

    holder.itemView.rb.setOnClickListener {
        if (list[position].id == 3) {
            Constants.carCleanOrFix = 0
            holder.itemView.rb.isChecked = false
        }
        if (list[position].id == 4) {
            Constants.carCleanOrFix = 1
            holder.itemView.rb.isChecked = false
        }
    }
}[enter image description here][1]

您需要在适配器类中具有一个全局变量,并将单击项目的位置分配到该变量。

/**
 * Created by subrahmanyam on 28-01-2016, 04:02 PM.
 */
public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.ViewHolder> {
private final String[] list;
private int lastCheckedPosition = -1;
public SampleAdapter(String[] list) {
    this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = View.inflate(parent.getContext(), R.layout.sample_layout, null);
    ViewHolder holder = new ViewHolder(view);
    return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.choiceName.setText(list[position]);
    holder.radioButton.setChecked(position == lastCheckedPosition);
}
@Override
public int getItemCount() {
    return list.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
    @Bind(R.id.choice_name)
    TextView choiceName;
    @Bind(R.id.choice_select)
    RadioButton radioButton;
    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastCheckedPosition = getAdapterPosition();
 //because of this blinking problem occurs so 
 //i have a suggestion to add notifyDataSetChanged();
             //   notifyItemRangeChanged(0, list.length);//blink list problem
                  notifyDataSetChanged();
            }
        });
    }
}
}

代码参考:https://stackoverflow.com/a/35060634/5846135

我解决了这个问题

var  selectedRBPosition = -1 // global variable for recyclerview adapter
override fun onBindViewHolder(holder: SearchCarViewHolder, position: Int) {
    holder.bind(list[position]) //for binding otherviews
    holder.itemView.rb.isChecked = selectedRBPosition ==position
    holder.itemView.rb.setOnClickListener {
        selectedRBPosition = holder.adapterPosition
        if (list[position].id == 3) {
            Constants.carCleanOrFix = 0
        }
        if (list[position].id == 4) {
            Constants.carCleanOrFix = 1
        }
        notifyDataSetChanged()
    }
}

感谢http://joshskeen.com/building-a-radiogroup-recyclerview/

相关内容

最新更新