Double Tap Opens two Android Activities

2


I'm having a problem in a list where I use RecyclerView , when clicking on an item in the list, OnClickListener is treated in ViewHolder of that RecyclerView :
MyViewHolder:

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 an item in the list it opens twice the same% with%. How do I disable this double-tap (something like OnDoubleClickListener return false)?

NOTE: If you have not understood that I will give more details if necessary.

    
asked by anonymous 09.05.2016 / 16:21

2 answers

0

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

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 it anywhere that I do not want to do the same action twice if double-clicked example:

// 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 a view it gives a timer (using the time I set) until I can perform an action again.

    
25.05.2016 / 16:52
0
Good morning, my friend. in my case below and I use a listview and I get 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 will work in RecyclerView .. but in my case double click does not work .. I hope I have helped ..

    
18.05.2016 / 15:21