![](/img/trans.png)
[英]One Select Radio button using multiple radio buttons on RecyclerView
[英]How To Create Android RecyclerView with one radio button in each item that all radio buttons can interact together?
当我选中单选按钮时,我无法在回收站视图的其他项目中取消选中其他单选按钮
单击单选按钮时无法到达其他项目视图位置
当我尝试取消选中其他单选按钮时,所有单选按钮都未选中..
覆盖乐趣 onBindViewHolder(holder: SearchCarViewHolder, position: Int) { holder.bind(list[position])
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]
您将需要在 Adapter 类中有一个全局变量,并将单击项目的位置分配给该变量。
/**
* 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();
}
});
}
}
}
我解决了这个问题
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()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.