-1

when I check radio button I can't uncheck other radio buttons in other items of recycler view

I can't reach other item view position on radio button click

when I tried to uncheck other radio buttons all radio buttons are unchecked ..

override fun 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]

2 Answers2

0

You will need to have a global variable in Adapter class and assign position of clicked item to that variable.

/**
 * 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();

            }
        });
    }
}
}

Code ref: https://stackoverflow.com/a/35060634/5846135

Zeero0
  • 2,602
  • 1
  • 21
  • 36
0

I got this problem solved with

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

thanks to http://joshskeen.com/building-a-radiogroup-recyclerview/