Differentiate Single click and double click of a imageView in android

前端 未结 5 991
南笙
南笙 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 10:50

    just a little change and my code works fine. I placed the mHasDoubleClicked boolean inside handler, that makes the trouble. The below code works.

    img_feat_orgn_item.setOnClickListener(new OnClickListener() {
         @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub
            findDoubleClick();
        if (mHasDoubleClicked) {
                            Log.v("double click","double click");
        startActivity(new Intent(
                            Wv_HomePage.this,
                            NewDonation.class));
                        }
            }
            });
    
        private boolean findDoubleClick() {
                // Get current time in nano seconds.
            long pressTime = System.currentTimeMillis();
                // If double click...
                if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
                    mHasDoubleClicked = true;           
    
                    // double click event....
                } else { // If not double click....
                    mHasDoubleClicked = false;
                    Handler myHandler = new Handler() {
                        public void handleMessage(Message m) {
    
                            if (!mHasDoubleClicked) {
                                // single click event
                                feature_class_val = listData_Feature_Organization
                                        .get(j);
                                Intent intent = new Intent(
                                        Wv_HomePage.this,
                                        OrganizationDetails.class);
                                Bundle b = new Bundle();
                                b.putString("orgn_name",
                                        feature_class_val.name);
                                intent.putExtras(b);
                                startActivity(intent);
    
                            }
                        }
                    };
                    Message m = new Message();
                    myHandler.sendMessageDelayed(m, DOUBLE_PRESS_INTERVAL);
                }
                lastPressTime = pressTime;
                return mHasDoubleClicked;
            }
    

提交回复
热议问题