0

I am implementing a RecycleView that contains two view types. One view type contains a TextView and other contains a RadioButton. I maintain an array list to keep radio button check/uncheck status. But sometimes RadioButton get uncheck even I set check status using array List on scrolling.

here is my adapter class

public class AddItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private ArrayList<Object> items=null;
private final int MENU_OPTION = 0;
private final int MENU_CHOICE = 1;
private Context context;
private int prevSelectedItem=-1;
private RadioButton prevSelectedRdBtn=null;


public AddItemsAdapter(Context context, ArrayList<Object> items) {
    this.items = items;
    this.context = context;
}


@Override
public int getItemCount() {
    return this.items.size();
}

@Override
public int getItemViewType(int position) {
    if (items.get(position) instanceof MenuOption) {
        return MENU_OPTION;
    } else if (items.get(position) instanceof MenuChoice) {
        return MENU_CHOICE;
    }
    else {
        return -1;
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

    switch (viewType) {
        case MENU_OPTION:
            View v1 = inflater.inflate(R.layout.menu_option, viewGroup, false);
            viewHolder = new VMMenuOptionHolder(v1);
            break;
        case MENU_CHOICE:
            View v2 = inflater.inflate(R.layout.menu_choice, viewGroup, false);
            viewHolder = new VMMenuChoiceHolder(v2);
            break;
        }
        return viewHolder;
    }

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
    try {
        switch (viewHolder.getItemViewType()) {
            case MENU_OPTION:
                VMMenuOptionHolder vh1 = (VMMenuOptionHolder) viewHolder;
                MenuOption dt= (MenuOption) items.get(i);
                vh1.title.setText(dt.getOptionTitle());
                vh1.choiceNote.setText(" - Select "+dt.getMaxChoiceAllowed()+" Choices");
                break;
            case MENU_CHOICE:
                final VMMenuChoiceHolder vh2 = (VMMenuChoiceHolder) viewHolder;
                MenuChoice mc= (MenuChoice) items.get(i);
                vh2.radioButton.setChecked(mc.isSelect());


                vh2.radioButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (prevSelectedItem!=i && prevSelectedRdBtn!=vh2.radioButton) {
                            selectChoice(i,vh2);
                        }
                    }
                });
                break;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void selectChoice(int pos, VMMenuChoiceHolder vh2) {
    MenuChoice mc=(MenuChoice) items.get(pos);
    mc.setSelect(true);
    vh2.choiceNote.setText(" - Select "+mc.getMaxChoiceAllowed()+" Choices");

    if(!mc.getSubMenuChoicesList().isEmpty()){
        items.addAll(pos+1,mc.getSubMenuChoicesList());
    }
    if (prevSelectedItem != -1 && prevSelectedRdBtn!= null) {
        ((MenuChoice) items.get(prevSelectedItem)).setSelect(false);
        prevSelectedRdBtn.setChecked(false);
    }
    notifyDataSetChanged();
    prevSelectedItem=pos;
    prevSelectedRdBtn=vh2.radioButton;
}

private class VMMenuChoiceHolder extends RecyclerView.ViewHolder{
    TextView choiceNote;
    RadioButton radioButton;
    CheckBox checkBox;

    VMMenuChoiceHolder(final View v2) {
        super(v2);
        radioButton=v2.findViewById(R.id.radioButton);
        checkBox=v2.findViewById(R.id.checkBox);
        choiceNote=v2.findViewById(R.id.choice_note);
    }
}

}

Darshana
  • 406
  • 5
  • 18

3 Answers3

0

use logic below on onBindView:

holder.attenCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (holder.attenCheckBox.isChecked())
                dataModel.setChecked(true);
            else
                dataModel.setChecked(false);
        }
    });

    if (dataModel.getChecked())
        holder.attenCheckBox.setChecked(true);
    else
        holder.attenCheckBox.setChecked(false);
    holder.checkboxLinearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (holder.attenCheckBox.isChecked())
                holder.attenCheckBox.setChecked(false);
            else
                holder.attenCheckBox.setChecked(true);


        }
    });

Explanation:

Recycle view inflate eveytime when you scroll down or up. you need to store a flag in the data pojo to keep track of check status using that flag with setOnCheckedChangeListener will enable you to have you checked enable/disable .make sure you put flag after listener.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0

Save the state of your radio button by giving them dynamic tags and saving there state to a hashmap on onChecked() method of radio button. And check there state while inflating recycle view. Please comment if more explanation is required..

Mehul Vasa
  • 51
  • 9
0

When you check radio button every time you have to save(you can save on an arraylist) the checked state. And every time of creating view (inside adapter) you have to check the state of radio button (check from arraylist where you save the states). If you get it is checked then you have to checked the radio button programmatically. Hope you understand well.

Mafujul
  • 1,090
  • 10
  • 15