My RecyclerView
contains radio buttons. when I click one my following implementation should deselect all the others (notifyDataSetChanged()
should rebind all the rows). Surprisingly, onClick
is never triggered:
public class NameAdapter extends RecyclerView.Adapter<NameAdapter.NameHolder> {
private List<String> myNames;
private int selectedPosition = -1;
public NameAdapter(List<String> names) {
myNames = names;
}
@Override public NameHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.name_item, parent, false);
return new NameHolder(view);
}
@Override public void onBindViewHolder(NameHolder holder, int position) {
String name = myNames.get(position);
holder.name.setText(name);
holder.radioButton.setChecked(position == selectedPosition);
}
@Override public int getItemCount() {
return myNames.size();
}
public class NameHolder extends RecyclerView.ViewHolder {
private TextView name;
private RadioButton radioButton;
public NameHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
radioButton = (RadioButton) itemView.findViewById(R.id.radio_button);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override public void onClick(View v) {
selectedPosition = getAdapterPosition();
notifyDataSetChanged();
}
};
itemView.setOnClickListener(clickListener);
}
}
}