"onClick" and "onLongClick" functions are called with a single touch

Asked

Viewed 121 times

0

I have a simple problem, but I cannot find a specific solution to this problem. Inside my application I have a Recycler View and in it I have two click events: one for simple touch and one for long touch. When I run the long tap it runs, but when I take my finger off the screen the simple tap is triggered too (which shouldn’t happen). I need the simple touch not to be called after the long touch runs.

These are the click events implemented on my Activity:

@Override
public void onClick(Tarefa tarefa) {
    startActivity(new Intent(MainActivity.this, EditarTarefaActivity.class));
}

@Override
public void onLongClick(Tarefa tarefa) {
    Toast.makeText(this, "on long click funcionando!!!", Toast.LENGTH_SHORT).show();
}

On my Adapter it is being called within the onBindViewholder method:

@Override public void onBindViewHolder(@Nonnull Viewholder Holder, int position) { final Task task = tasks.get(position); Holder.bind(task);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onClick.onClick(tarefa);
        }
    });

    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            onClick.onLongClick(tarefa);
            return false;
        }
    });
}

1 answer

1


According to the documentation of setOnLongClickListener, you need to return true an action is taken, false if it doesn’t happen.

Passing by false, it is interpreted that the function was not executed, then the setOnClickListener runs, causing the problem you described.

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        onClick.onLongClick(tarefa);
        return true; // Aqui
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.