Differentiate Single click and double click of a imageView in android

前端 未结 5 990
南笙
南笙 2021-01-29 10:24

I have tried the following code to differentiate single click and double click. Single click is ok. When I double click the imageview, code inside both the single click and doub

5条回答
  •  悲&欢浪女
    2021-01-29 11:11

    Try this.

       btn.setOnClickListener(new View.OnClickListener() {
    
            volatile int i = 0;
    
            @Override
            public void onClick(View v) {
    
                i++;
                Handler handler = new Handler();
    
                Runnable r = new Runnable() {
                    @Override
                    public void run() {
                        if (i == 1) {
                            //single click logic
                        }
                    }
                };
    
                if (i == 1) {
                    handler.postDelayed(r, 150);
                } else if (i == 2) {
                    handler.removeCallbacks(r);
                    i = 0;
                    //Double click logic
                }
            }
        }
        );
    

    Or you can check DOUBLE-TAP example from following URL. that is used in listView. i hope it is useful for you.

    https://nodeload.github.com/NikolaDespotoski/DoubleTapListView/zip/master

提交回复
热议问题