[英]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.