How can I unselect all the selected items in the recycler view with single button click in android studio using JAVA

465 views Asked by At

I have selected and unselected the list using simple onclicklistener action. I also have the selected items array. Now how can I unselect all at the same time with a button click. Thank you in advance

This is the adapter of my recycler view

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


MainActivity activity;
final ArrayList<NameModel> nameModels;
ArrayList<NameModel> selected= new ArrayList<>();
boolean isSelect = false;

public NameAdapter(MainActivity activity, ArrayList<NameModel> NameModel){

    this.activity=activity;
    this.nameModels=NameModel;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.row_view,parent,false);

    return new ViewHolder(view);

}

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

    holder.textView.setText(nameModels.get(position).getName());
    holder.textView2.setText(nameModels.get(position).getDistance());


}

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

public int getSelectedCount() {
    Log.d("selected "," "+ selected.size());
    return selected.size();
}



class ViewHolder extends RecyclerView.ViewHolder{


    ImageView imageView;
    TextView textView;
    TextView textView2;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        imageView = itemView.findViewById(R.id.image);
        textView = itemView.findViewById(R.id.textView);
        textView2 = itemView.findViewById(R.id.textView2);


        itemView.setOnLongClickListener(view -> {
            isSelect=true;
            if (selected.contains(nameModels.get(getAdapterPosition()))){
                itemView.setBackgroundColor(Color.TRANSPARENT);
                imageView.setVisibility(View.INVISIBLE);
                selected.remove(nameModels.get(getAdapterPosition()));
                activity.setCount();

            }
            else {
                itemView.setBackgroundColor(Color.rgb(231, 240, 255));
                imageView.setVisibility(View.VISIBLE);
                selected.add(nameModels.get(getAdapterPosition()));
                activity.setCount();


            }
            if (selected.size() == 0){
                isSelect=false;
            }
            return true;
        });


        itemView.setOnClickListener(view -> {
            if (isSelect) {
                if (selected.contains(nameModels.get(getAdapterPosition()))) {
                    itemView.setBackgroundColor(Color.TRANSPARENT);
                    imageView.setVisibility(View.INVISIBLE);
                    selected.remove(nameModels.get(getAdapterPosition()));
                    activity.setCount();

                } else {
                    itemView.setBackgroundColor(Color.rgb(231, 240, 255));
                    imageView.setVisibility(View.VISIBLE);
                    selected.add(nameModels.get(getAdapterPosition()));
                    activity.setCount();

                }


                Log.d("selected"," "+selected.size());
                if (selected.size() == 0) {
                    isSelect = false;
                }
            }
        });


        }
    }


}

This is the namemodel class.

public class NameModel implements Serializable {

String name;
String distance;

public NameModel(String name, String distance) {
    this.name = name;
    this.distance = distance;
}

public String getName() {
    return name;
}

public String getDistance() {
    return distance;
}}

This is the code in my Mainactivity for recycler view.

RecyclerView recyclerView = findViewById(R.id.mRecyclerView);
        setNameModels();

    adapter = new NameAdapter(this, nameModels);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

private void setNameModels() {
    String[] name = getResources().getStringArray(R.array.name);
    String[] distance =getResources().getStringArray(R.array.distance);

    for (int i = 0; i < name.length; i++) {
        nameModels.add(new NameModel(name[i], distance[i]));
    }
}
0

There are 0 answers