Double Tap Opens Two Activitys Android

Asked

Viewed 420 times

2


I’m having a problem with a list where I use a RecyclerView, by clicking on an item in the list, OnClickListener is dealt with in the ViewHolder that RecyclerView:
Meuviewholder:

public ClientesViewHolder(Context ctx, View itemView, Activity act) {
   super(itemView);
   itemView.setOnClickListener(this);
}

@Override
 public void onClick(View v) {
    Intent it = new Intent(ctx, TelaDadosClientes.class);
    Bundle animacao = ActivityOptionsCompat.makeCustomAnimation(ctx, R.anim.slide_in_left, R.anim.slide_out_left).toBundle();
    ActivityCompat.startActivity(activity, it, animacao);
 }

Clicking on an item in the list opens a new activity with an animation (which is not the case).
My problem is that if I double-click a list item it opens twice the same activity. Can I disable this double touch (something like Ondoubleclicklistener Return false)?

OBS: In case you don’t understand comment I will give more details if necessary.

  • I do not know your procedure, but to prevent an Activity open again on the screen I use intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); I hope it helps

  • Hello @Armando didn’t work, keep opening two activitys if I double click on an item.

  • And FLAG_ACTIVITY_CLEAR_TOP ?

3 answers

0

Following Bruno’s logic there is an alternative way to avoid double click as well. Create a long type variable in your Adapter

  • long private mLastClickTime = 0;

And in the onClick method of the item in your Adapter you check the click time, if you are respecting a 1000 millisecond interval or the time set by you

@Override
public void onClick(View v) 
{
    //verifique se o tempo do último click tem um intervalo de 1000 milessegundos
    if(SystemClock.elapsedRealtime() - mLastClickTime < 1000)
    {
        return;
    }

    mLastClickTime = SystemClock.elapsedRealtime();
}

0

Good morning buddy in my case below and use a listview and pick the selected item as follows..

ltsunidades.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView adapter, View view, int posicao, long id) {
                Tab_UC obj = (Tab_UC) adapter.getItemAtPosition(posicao);
                String filial = "" + obj.getCod_UC();
                Intent it = new Intent(getBaseContext(), Empresa.class);
                it.putExtra("Filial", filial);
                startActivity(it);

            }
        });

I do not know if this way will work in Recyclerview.. however in my case the double click does not work.. hope I’ve helped..

  • Actually (logically speaking) your code is doing nothing other than mine. I don’t see how this can help me.

  • Yes it changes when you call the onclick in my case it is onitemclick a look at it

  • onItemClick() does not work in Recyclerview. And if I were to use your code, I would take the first two lines of onItemClick() and just use Intent to open my Activity, which I already do but using an animation.

  • yes yes I never used Recyclerview sorry I can’t help then =(

  • At least he tried, I just can’t believe no one’s been through it kkk, like on a Button if I give him two quick touches he opens two Activitys !!

0


I was able to solve my problem by creating a static function similar to a timer:

Utils.java

private static int clk = 0;

public static boolean testClique(int ms) {

    Handler handler = new Handler();
    Runnable r = new Runnable() {
        @Override
        public void run() { 
          clk = 0; 
        }
    };

    if (clk == 0) {
        clk = 1;
        handler.postDelayed(r, ms);
        return true;
    }
    handler.postDelayed(r, 1000);
    return false;
}

So I can call her anywhere I don’t want to run the same action twice if a double-click example is given:

// Esse era meu problema ao dar dois toques rápidos em um item do recyclerView
// ele abria duas vezes a mesma activity

if (Utils.testClique(1000)) { // Envio um intervalo de 1s para poder clicar de novo
     Intent it = new Intent(contexto, MinhaActivity.class);
     Bundle animacao = ActivityOptionsCompat...;
     ActivityCompat.startActivity(activity, it, animacao);
}
// Também uso esta função em Buttons, Toolbar Itens, e onde mais eu quiser

Now when I click on one view it gives a timer (using the time I set) until it can perform an action again.

Browser other questions tagged

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