Allow user to select only 3 items in recyclerview

Basically I want user to select only 3 items from a list in recyclerview

My adapter class holder onclicklistener code:

holder.addproduct.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        ld.setSelected(!ld.isSelected());

        if (ld.isSelected()) {
            holder.addproduct.setImageDrawable(v.getResources().getDrawable(R.drawable.ic_checked));
        } else {
            holder.addproduct.setImageDrawable(v.getResources().getDrawable(R.drawable.ic_add_black_24dp));
        }

    }
});

My model class:

public class Product {
    private boolean isSelected = false;

    public Product() {

    }

    public Product(boolean isSelected) {
        this.isSelected = isSelected;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }
}

isSelected is used to get value of selected items in fragment. Any help would be appreciated.



Read more here: https://stackoverflow.com/questions/67921672/allow-user-to-select-only-3-items-in-recyclerview

Content Attribution

This content was originally published by user2294087 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: