Hashir Saeed
Hashir Saeed

Reputation: 245

Select All Items in a Recycler View with click on single item

I will Explain my question a little bit. There's a checkbox in my recycler view layout, who's is invisible at the start. When I click on one of the items of recycler view, I want the checkbox to be visible in all the items. I have searched the internet for too long, and can't find a workaround for my problem.


public class ViewInventoryListAdapter extends RecyclerView.Adapter<ViewInventoryListAdapter.ListViewHolder> {

private Context context;
List<ViewAllInventoryDevicesDetails> list;
private Button assignButton;
private UserLoginResponseModel userLoginResponseModel;
private android.support.v4.app.Fragment fragment;
private FragmentManager fragmentManager;
private FragmentActivity myContext;
private Activity activity;
private int counter = 0;
private List<String> selectedDeviceSerialNumbers;
private static CheckBox checkBox;

public ViewInventoryListAdapter(Context context, List<ViewAllInventoryDevicesDetails> list, Button assign, UserLoginResponseModel model, Activity activity ) {
    this.context = context;
    this.list = list;
    this.assignButton = assign;
    this.userLoginResponseModel = model;
    this.activity =activity;
}

@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.view_all_inventory_listlayout_cardview,null);
    //Specifying Activity for Fragment Transaction
    activity = activity;
    myContext = (FragmentActivity) activity;
    //Iniallizing Fragment Manager
    fragmentManager = myContext.getSupportFragmentManager();
    selectedDeviceSerialNumbers = new ArrayList<>();
    checkBox =  view.findViewById(R.id.view_inventory_checkbox);
    return new ViewInventoryListAdapter.ListViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ListViewHolder holder, final int position) {
    Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);
    holder.deviceSerialNumber.setText(list.get(position).getSerialNumber());
    holder.deviceType.setText(list.get(position).getDeviceType());
    FontManager.markAsIconContainer(holder.deviceIcon, iconFont);
    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {

            holder.isSelected.setVisibility(View.VISIBLE);
            //holder.isSelected.setVisibility(View.VISIBLE);
            holder.isSelected.setChecked(true);

            assignButton.setVisibility(View.VISIBLE);

            return false;
        }
    });
  holder.isSelected.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          holder.isSelected.setChecked(false);
          holder.isSelected.setVisibility(View.INVISIBLE);
          //Login For Visibility For assign Button
          counter--;
          selectedDeviceSerialNumbers.remove(list.get(position).getSerialNumber());
          if(counter == 0){
              assignButton.setVisibility(View.GONE);
          }
      }
  });

  assignButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          Bundle bundle = new Bundle();
          if(userLoginResponseModel != null){
              bundle.putSerializable("UserModel", userLoginResponseModel);
              bundle.putSerializable("serialNumbers", (Serializable) selectedDeviceSerialNumbers);
          }
          fragment = new AssingSelectedDevicesFragment();
          fragment.setArguments(bundle);
          fragmentManager.beginTransaction()
                  .replace(R.id.flContent, fragment)
                  .commit();
      }
  });

}

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

class ListViewHolder extends RecyclerView.ViewHolder{

    private TextView deviceType, deviceSerialNumber, deviceIcon;
    private CheckBox isSelected;

    public ListViewHolder(View itemView){
        super(itemView);
        deviceType = itemView.findViewById(R.id.view_inventory_deviceType_textview);
        deviceSerialNumber = itemView.findViewById(R.id.view_inventory_serialNumber_textview);
        deviceIcon         = itemView.findViewById(R.id.view_inventory_image_textview);
        isSelected         = itemView.findViewById(R.id.view_inventory_checkbox);
    }

    }

 }

This is my OnBindViewHolder Text. I can change the checkbox visibility by clicking on item individually. I just want it to change for all items on the single click. Just Like the Contact list in our Mobile. you just long press on item and checkboxes appear in all of the items. Any help would be appreciated. Best Regards

Upvotes: 4

Views: 5882

Answers (3)

Nilesh Panchal
Nilesh Panchal

Reputation: 1069

Hello you can also achieve this by using callback (Interface)

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ListViewHolder> {

    ArrayList<ListItemModel> myArrayList;
    ListClicklistener listClicklistener;

    public MyListAdapter(ArrayList<ListItemModel> myArrayList, ListClicklistener listClicklistener) {
        this.myArrayList = myArrayList;
        this.listClicklistener = listClicklistener;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ListViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ListViewHolder listViewHolder, int i) {
        listViewHolder.tvTitle.setText(myArrayList.get(i).title);

        if (myArrayList.get(i).isCheckBoxShow) {
            listViewHolder.chkBox.setVisibility(View.VISIBLE);
        }
        listViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listClicklistener.onListItemClick();
            }
        });
    }

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

    public class ListViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        CheckBox chkBox;

        public ListViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            chkBox = itemView.findViewById(R.id.chkBox);
        }
    }
}

Take One Interface

public interface ListClicklistener {
    void onListItemClick();
}

SetAdapter with Callback

myListAdapter = new MyListAdapter(myArrayList, new ListClicklistener() {
            @Override
            public void onListItemClick() {
                for (int i = 0; i < myArrayList.size(); i++) {
                    myArrayList.get(i).isCheckBoxShow = true;
                }
                myListAdapter.notifyDataSetChanged();
            }
        });

Upvotes: 0

Sunny
Sunny

Reputation: 3265

you need to add one boolean variable in ViewAllInventoryDevicesDetails class to check is item selected or not and the checkbox selected or not boolean isCheckSelected= false; // Default value will be false because in starting no value has selected.

Here I modified you code:

public class ViewInventoryListAdapter extends RecyclerView.Adapter<ViewInventoryListAdapter.ListViewHolder> {

    private Context context;
    List<ViewAllInventoryDevicesDetails> list;
    private Button assignButton;
    private UserLoginResponseModel userLoginResponseModel;
    private android.support.v4.app.Fragment fragment;
    private FragmentManager fragmentManager;
    private FragmentActivity myContext;
    private Activity activity;
    private int counter = 0;
    private List<String> selectedDeviceSerialNumbers;
    private static CheckBox checkBox;
    private int selectedItem = -1; // -1 due to first time nothing selected


    public ViewInventoryListAdapter(Context context, List<ViewAllInventoryDevicesDetails> list, Button assign, UserLoginResponseModel model, Activity activity) {
        this.context = context;
        this.list = list;
        this.assignButton = assign;
        this.userLoginResponseModel = model;
        this.activity = activity;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.view_all_inventory_listlayout_cardview, null);
        //Specifying Activity for Fragment Transaction
        activity = activity;
        myContext = (FragmentActivity) activity;
        //Iniallizing Fragment Manager
        fragmentManager = myContext.getSupportFragmentManager();
        selectedDeviceSerialNumbers = new ArrayList<>();
        checkBox = view.findViewById(R.id.view_inventory_checkbox);
        return new ViewInventoryListAdapter.ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ListViewHolder holder, final int position) {
        Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);
        holder.deviceSerialNumber.setText(list.get(position).getSerialNumber());
        holder.deviceType.setText(list.get(position).getDeviceType());
        FontManager.markAsIconContainer(holder.deviceIcon, iconFont);

        if (selectedItem != -1 && selectedItem == position) {
            holder.isSelected.setVisibility(View.VISIBLE);
            //holder.isSelected.setVisibility(View.VISIBLE);
            assignButton.setVisibility(View.VISIBLE);
        } else {
            holder.isSelected.setVisibility(View.GONE);
            assignButton.setVisibility(View.GONE);
        }

        if (selectedDeviceSerialNumbers.get(position).getCheckSelected()) {
            holder.isSelected.setChecked(true);
        } else {
            holder.isSelected.setChecked(true);
        }


        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (selectedItem != -1) {
                    selectedItem = position;
                    showAllBoxes();
                } else {
                    selectedItem = -1;
                    hideAllBoxes();
                }


                return false;
            }
        });
        holder.isSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedDeviceSerialNumbers.get(position).setCheckSelected(!selectedDeviceSerialNumbers.get(position).getCheckSelected());
            notifyDataSetChanged(); 
            }
        });

        assignButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                if (userLoginResponseModel != null) {
                    bundle.putSerializable("UserModel", userLoginResponseModel);
                    bundle.putSerializable("serialNumbers", (Serializable) selectedDeviceSerialNumbers);
                }
                fragment = new AssingSelectedDevicesFragment();
                fragment.setArguments(bundle);
                fragmentManager.beginTransaction()
                        .replace(R.id.flContent, fragment)
                        .commit();
            }
        });

    }

    private void showAllBoxes() {
        for (ViewAllInventoryDevicesDetails item : list) {
            item.showCheckbox = true;
        }
        notifyDataSetChanged();
    }

    private void hideAllBoxes() {
        for (ViewAllInventoryDevicesDetails item : list) {
            item.showCheckbox = false;
        }
        notifyDataSetChanged();
    }

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

    class ListViewHolder extends RecyclerView.ViewHolder {

        private TextView deviceType, deviceSerialNumber, deviceIcon;
        private CheckBox isSelected;

        public ListViewHolder(View itemView) {
            super(itemView);
            deviceType = itemView.findViewById(R.id.view_inventory_deviceType_textview);
            deviceSerialNumber = itemView.findViewById(R.id.view_inventory_serialNumber_textview);
            deviceIcon = itemView.findViewById(R.id.view_inventory_image_textview);
            isSelected = itemView.findViewById(R.id.view_inventory_checkbox);
        }

    }
}

I hope it will helps you.

Upvotes: 1

TheWanderer
TheWanderer

Reputation: 17824

This is how I do things like this:

Add a new field to your ViewAllInventoryDevicesDetails class:

public boolean showCheckbox = false;

Add some helper methods to your adapter:

private void showAllBoxes() {
    for (ViewAllInventoryDevicesDetails item : list) {
        item.showCheckbox = true;
    }
    notifyDataSetChanged();
}

private void hideAllBoxes() {
    for (ViewAllInventoryDevicesDetails item : list) {
        item.showCheckbox = false;
    }
    notifyDataSetChanged();
}

Then, in your onBindViewHolder() method, use that flag:

@Override
public void onBindViewHolder(@NonNull final ListViewHolder holder, final int position) {
     ViewAllInventoryDevicesDetails item = list.get(position);
     holder.isSelected.setVisibility(item.showCheckbox ? View.VISIBLE : View.GONE);
}

You'll simply need to call showAllBoxes() when you want to show them all and hideAllBoxes() to hide them again.

Upvotes: 6

Related Questions