Issue
In my RecyclerView
item, there is a button along with other views. I am hiding the button when it is clicked. Problem is, if I click the button on 1st item, the button on 8th item is auto clicked, if I click button on 2nd item, button on 9th item is auto clicked & so on. How to solve this problem?
Adapter class :
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<Model> models;
Model model;
// public MyAdapterListener onClickListener;
SparseBooleanArray mStateButtons = new SparseBooleanArray();
public Adapter(List<Model> models){
this.models = models;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
String question = models.get(position).getQues();
final String optA = models.get(position).getOptA();
final String optB = models.get(position).getOptB();
final String optC = models.get(position).getOptC();
final String optD = models.get(position).getOptD();
final String answer = models.get(position).getAns();
holder.question.setText(question);
holder.optA.setText(optA);
holder.optB.setText(optB);
holder.optC.setText(optC);
holder.optD.setText(optD);
holder.options.setTag(position);
holder.options.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = group.getCheckedRadioButtonId();
int clickedPos = (Integer) group.getTag();
models.get(clickedPos).setChecked(radioButtonID);
}
});
holder.options.check(models.get(position).getChecked());
final int currentPosition = holder.getAdapterPosition();
final Button button = holder.seeAnswer;
if(mStateButtons.valueAt(currentPosition)) {
button.setVisibility(View.GONE);
} else {
button.setVisibility(View.VISIBLE);
}
holder.seeAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mStateButtons.put(position, true);
}
});
}
@Override
public int getItemCount() {
return Models.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView question;
RadioButton optA, optB, optC, optD;
Button seeAnswer;
RadioGroup options;
public ViewHolder(View itemView) {
super(itemView);
options = (RadioGroup) itemView.findViewById(R.id.rgMcqOptions);
question = (TextView) itemView.findViewById(R.id.tvMcqQues);
optA = (RadioButton) itemView.findViewById(R.id.rbOptA);
optB = (RadioButton) itemView.findViewById(R.id.rbOptB);
optC = (RadioButton) itemView.findViewById(R.id.rbOptC);
optD = (RadioButton) itemView.findViewById(R.id.rbOptD);
seeAnswer = (Button) itemView.findViewById(R.id.btnSeeAnswer);
}
}
}
Solution
You need some way to keep track of which buttons should be hidden and which should not. This is the responsibility of your adapter, so you need to add some form of array to keep track of button states there. A SparseBooleanArray
is an efficient and appropriate option:
private SparseBooleanArray hideButtons = new SparseBooleanArray();
In onBindView
you need to update the view for the current item being bound, including updating the button visibility:
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.seeAnswer.setVisibility( hideButtons.get(position, false) ? View.GONE : View.VISIBLE );
...
}
And of course you need to actually set the visibility - and store it in the SparseBooleanArray
when clicking the button. Putting this event handler in the ViewHolder
is a good option:
class ViewHolder extends RecyclerView.ViewHolder{
Button seeAnswer;
...
ViewHolder(View itemView) {
super(itemView);
seeAnswer = (Button) itemView.findViewById(R.id.btnSeeAnswer);
seeAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
seeAnswer.setVisibility(View.GONE);
hideButtons.put(getAdapterPosition(), true);
}
});
...
}
}
This is a tested and verified solution, so if you follow this and it still doesn't work, the cause of your problem is somewhere else.
Answered By - Magnus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.