javaandroidandroid-recyclerviewandroid-checkbox

Select one checkBox in item from recyclerView and deselect previously selected checkBox


I have recycler-view with sections. The item include row and title. I want to select one item with checkbox and if user select next one I want to deselect the old the one(checkbox). I cannot find anywhere to achieve this functionality.

Here is my code

recyclerView = (RecyclerView)findViewById(R.id.recyclerViewActivityExample);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    list = new ArrayList<>();
    list.add(new AnimalObject("Cat","Mammals", false));
    list.add(new AnimalObject("Lion","Mammals", false));
    list.add(new AnimalObject("Dog","Mammals", false));
    list.add(new AnimalObject("Monkey","Mammals", false));
    list.add(new AnimalObject("Puma","Mammals", false));

    list.add(new AnimalObject("Albatross","Birds", false));
    list.add(new AnimalObject("Pigeon","Birds", false));

    list.add(new AnimalObject("Crabs","Aquatic Animals", false));
    list.add(new AnimalObject("Sharks","Aquatic Animals", false));

    MyAdapter myAdapter = new MyAdapter();


    sectionedRecyclerViewAdapter = new SectionedRecyclerViewAdapter(getApplicationContext(),
            R.layout.layout_list_section, R.id.textViewItemSection, myAdapter, this);
    sectionedRecyclerViewAdapter.setSections(list);

    recyclerView.setAdapter(sectionedRecyclerViewAdapter);

}





 //-------------------Adapter----------------------------
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements MyViewHolder.ViewHolderClickListener {

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.layout_list_item, parent, false);

        return new MyViewHolder(itemView,this);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        AnimalObject animalObject = list.get(position);

        holder.title.setText(animalObject.name);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // checkbox.setChecked(true);
                row_index = position;
                notifyDataSetChanged();
            }
        });

        if (row_index == position) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }
    }

    @Override
    public void onClick(View v) {

        Toast.makeText(getApplicationContext(), list.get(sectionedRecyclerViewAdapter.getIndexForPosition(recyclerView.getChildPosition(v))).name,Toast.LENGTH_SHORT).show();

    }
}

// Adapter

 public String name;
public String type;
public boolean ischecked ;

public AnimalObject(final String name, final String type, boolean ischecked){

    this.name = name ;
    this.type = type ;
    this.ischecked = ischecked;
}
public boolean ischecked() {
    return ischecked;
}

public void setIschecked(boolean ischecked) {
    this.ischecked = ischecked;
}

Solution

  • Inside your Adapter

    use this :
    private int row_index;

       @Override
        public void onBindViewHolder(final Main_Page_Payment_Cash_Adapter.ViewHolder holder, final int position) {
    
                 holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // checkbox.setChecked(true);
                        row_index = position;
                        notifyDataSetChanged();
                    }
                });
    
                if (row_index == position) {
                  // checkbox.setChecked(true);
                } else {
                   // checkbox.setChecked(false);
                }
        }
    

    I hope this is what you are looking for.