Nested RecyclerView with boolean array?

Viewed 70

I am working on an app with a dynamic number of switches to connect to a device. Here is my model class:

public class SwitchModel {

private String product;
private String deviceId;
private ArrayList<Boolean> switchPool;

public SwitchModel(String product, String deviceId, ArrayList<Boolean> switchPool) {
    this.product = product;
    this.deviceId = deviceId;
    this.switchPool = switchPool;
}

As you can see, I have only a product string for user input. A device id provided by the API and a dynamic number of switches. I want to my final app functions like this:

enter image description here

I tried to do this task by using a nested RecyclerView:

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {

Activity context;
private ArrayList<SwitchModel> modelList;

public MainAdapter(Activity context, ArrayList<SwitchModel> modelList) {
    this.context = context;
    this.modelList = modelList;
}
@Override
public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
    SwitchModel model = modelList.get(position);
    holder.productName.setText(model.getProduct());
    ChildAdapter childAdapter = new ChildAdapter(model.getSwitchPool());
    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
    holder.rvChild.setLayoutManager(layoutManager);
    holder.rvChild.setAdapter(childAdapter);
}

And here is the code for my Child Adapter:

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

private ArrayList<Boolean> switches;

public ChildAdapter(ArrayList<Boolean> switches) {
    this.switches = switches;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    Boolean model = switches.get(position);

    holder.switchButton.setChecked(model);

    holder.switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(SwitchButton view, boolean isChecked) {
            Log.i("myResponse", "onCheckedChanged: " + view.isChecked());
            return;
        }
    });
}

The RecyclerView sets the checked value of the switch correctly. But my problem is as you can see in my child view I have bound a boolean for each index of array list, but when I call for a check change listener it does not return the exact switch number of the product. It would be great if anyone could help me with this?

1 Answers

I would perhaps try to leverage a ConcatAdapter from Google.

In essence, you can supply N adapters with N data and update/handle them individually. I have never tried to do this by myself, and I don't want to make it sound like it's an easy thing to do, but it appears to "perhaps fit the use-case" you describe.

Someone has already done an expandable recyclerview using concat adapter so perhaps this would point you in the right direction before you get your hands dirty trying to make it work.

Related