Search code examples
androideventsandroid-recyclerviewonclicklisteneronlongclicklistener

Can I switch between onClickListener and onLongClickListener on the run time?


I added OnClickListener and OnLongClickListener for the item in the ViewHolder class of the RecyclerView's adapter

The idea is basically, when I want to select an item I should long click on it for the first time then I should do a normal short click to select the other items

The following is the code for the view holder with the interfaces for the long click and the normal click The interfaces will be implemented by the fragment's class and the method of the interfaces will be overriden. As the code shows, I make if-condition that changes between the long click and the normal click linstener according to MultipleSelectionOn (Which is a boolean variable). However, when I click a long click on the item, the MultipleSelectionOn variable changes to true but the onClickListner is not executed.

public class DevicesViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener{

    LinearLayout device_item_layout;
    TextView title,status;
    ImageView deleteIcon, selectionBtn;

    OnItemClick onItemClick;
    OnItemLongClick onItemLongClick;

    public DevicesViewHolder(@NonNull View itemView, OnItemClick onItemClick, OnItemLongClick onItemLongClick) {
        super(itemView);

        device_item_layout = itemView.findViewById(R.id.device_item);
        title = itemView.findViewById(R.id.title);
        status = itemView.findViewById(R.id.status);
        deleteIcon = itemView.findViewById(R.id.deleteIcon);
        selectionBtn = itemView.findViewById(R.id.selectionBtn);

        this.onItemClick = onItemClick;
        this.onItemLongClick = onItemLongClick;

        if(MultipleSelectionOn == false) {
            itemView.setOnClickListener(null);
            itemView.setOnLongClickListener(this);
        }else{
            itemView.setOnLongClickListener(null);
            itemView.setOnClickListener(this);
        }

    }

    @Override
    public boolean onLongClick(View view) {

        Log.i("MultipleSelectionOn ", String.valueOf(MultipleSelectionOn));

        onItemLongClick.onItemLongClick(getAdapterPosition(), view);

        MultipleSelectionOn = true;

        return true;
    }

    @Override
    public void onClick(View view) {

        Log.i("MultipleSelectionOn ", String.valueOf(MultipleSelectionOn));

        onItemClick.onItemClick(getAdapterPosition(), view);

    }
}



public interface OnItemLongClick{

    void onItemLongClick(int position, View view);

}

public interface OnItemClick{

    void onItemClick(int position, View view);

}

Solution

  • At first initialise both setOnLongClickListener and setOnClickListener. And for the action there are multiple ways you can do that -

    1. Create a view that overlaps the recyclerView's item when user long press it. It'll mimic the selection of an Item. And also take a flag like isSelected. The value of flag will be changed when user long press an item. Add a condition in setOnClickListener that if isSelected is not equal to true then execute whatever inside the click listener. The flag isSelected will be in the adaptor.

    2. You can take that isSelected in put it in the modal class of which the recyclerView implemented. And in setOnLongClickListener update its value to true or false accordingly. And then add a condition in setOnClickListener that if isSelected is not equal to true then execute whatever inside the click listener. But this time you use the value of isSelected associated with object of modal class at that specific position.