2014-01-21 59 views
-1

我試過以下代碼來區分單擊和雙擊。單擊即可。當我雙擊imageview時,單擊和雙擊部分內的代碼都會執行。區分單擊並雙擊androidView中的imageView

private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis 
    private long lastPressTime; 
    boolean mHasDoubleClicked; 

img_feat_orgn_item.setOnClickListener(new OnClickListener() { 
@Override 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    findDoubleClick(); 
    } 
    }); 

private boolean findDoubleClick() { 
     // Get current time in nano seconds. 
    long pressTime = System.currentTimeMillis(); 
     // If double click... 
     if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) { 
      mHasDoubleClicked = true;   
      startActivity(new Intent(
        Wv_HomePage.this, 
        NewDonation.class)); 
      // double click event.... 
     } else { // If not double click.... 
      mHasDoubleClicked = false; 
      Handler myHandler = new Handler() { 
       public void handleMessage(Message m) { 
        boolean mHasDoubleClicked = false; 
        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; 
    } 

回答

1

只是一點點的變化,我的代碼工作正常。我放置了mHasDoubleClicked布爾內部處理程序,這會造成麻煩。下面的代碼工作。

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; 
     } 
0

試試這個。

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 
      } 
     } 
    } 
    ); 

或者,您可以從以下網址檢查-TAP例如。在listView中使用的 。我希望它對你有用。

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

+0

在你的代碼中我可以放單和雙擊代碼? – Manikandan

+0

@Manikandan我編輯答案。重新檢查。 –

+0

我用你的代碼,當我雙擊時,單擊內的代碼也執行 – Manikandan

1

這是我的代碼,它似乎工作。

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.widget.ImageView; 

public class MultiTapImageView extends ImageView{ 

    private GestureDetector gestureDetector; 
    private MultiTapImageViewListener mListener; 

    public interface MultiTapImageViewListener { 

     void onDoubleTap(); 
     void onSingleTap(); 

    } 

    public MultiTapImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     gestureDetector = new GestureDetector(context, new GestureListener()); 

    } 

    public void setDoubleTapListener(MultiTapImageViewListener listener){ 
     mListener = listener; 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent e) { 
     return gestureDetector.onTouchEvent(e); 
    } 


    private class GestureListener extends GestureDetector.SimpleOnGestureListener { 


     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 
     // event when double tap occurs 
     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      float x = e.getX(); 
      float y = e.getY(); 

      if(mListener != null){ 
       mListener.onDoubleTap(); 
      } 

      return true; 
     } 


     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) { 
      if(mListener != null){ 
       mListener.onSingleTap(); 
      } 

      return true; 
     } 
    } 
}