2

How to disable onClick when I click for LongClick? It's a code from recyclerView, when I try long click I just see that normal click just spamming like hell.

 holder.title.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "To clear your recomendations, press for few seconds. ", Toast.LENGTH_SHORT).show();
                    }
                });
                holder.title.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        clearPreferences(R.string.preferences_reminder);
                        Toast.makeText(context, "Recomendations cleared.", Toast.LENGTH_SHORT).show();


                        return true;
                    }
                });
mnille
  • 1,328
  • 4
  • 16
  • 20
Rodriquez
  • 981
  • 1
  • 7
  • 21

2 Answers2

3

In case if there is necessity to listen both onLongClick and onClick, Here is another approach:

Example:

@Override
public boolean onLongClick(View view) {
    //return value true to make sure only onLongClick is executed without triggering normal onClick
    return true; // or false
}

return true means that the event is consumed. It is handled. No other click events will be notified.
return false means the event is not consumed. Any other click events will continue to receive notifications.

So to make sure both onClick and onLongClick are not triggered at the same time, return true from onLongClick event.

miken32
  • 42,008
  • 16
  • 111
  • 154
Vijay E
  • 829
  • 10
  • 14
0

Add this line

holder.title.setOnClickListener(null);
Nikhil PV
  • 1,014
  • 2
  • 16
  • 29