2016-02-08 55 views
0

我在使用GestureDetector和可點擊的RelativeLayout時遇到了問題。當我的RelativeLayout不可點擊時,應用程序完全識別每個手勢,但是當我將佈局設置爲可點擊時,應用程序不會識別手勢。 有什麼辦法可以同時使用兩者嗎?使用手勢檢測器和可點擊的RelativeLayout

回答

0

不幸的是,onClick()& onTouch()在一起使用時無法正常運行。 我使用解決方法來檢測使用onTouch的點擊。 我不確定它是如何與手勢檢測器存在。 我會分享我的代碼..它沒有手勢檢測器工作正常。讓我知道如果它不工作。

初始化聽衆:

GestureDetector gdt; 
View.OnTouchListener touchListener; 
private void initializeListener() 
{ 
    gdt = new GestureDetector(mContext,new GestureListener()); 
    touchListener = new View.OnTouchListener() { 
     float startX; 
     float startY; 
     float CLICK_ACTION_THRESHOLD = 5; 
     //Uses the touched coordinates recorded through MOTION_DOWN & MOTION_UP to detect whether the action performed was a click 
     private boolean isAClick(float startX, float endX, float startY, float endY) { 
      if(isLoadingView) 
       return false; 

      float differenceX = Math.abs(startX - endX); 
      float differenceY = Math.abs(startY - endY); 
      return !(differenceX > CLICK_ACTION_THRESHOLD || differenceY > CLICK_ACTION_THRESHOLD); 
     } 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        startX = event.getX(); 
        startY = event.getY(); 
        break; 
       case MotionEvent.ACTION_UP: { 
        float endX = event.getX(); 
        float endY = event.getY(); 

        if (isAClick(startX, endX, startY, endY)) 
        { 
         switch (v.getId()) 
         { 
          case R.id.view_id: 
          break 

         } 
        } 
       } 
       break; 
      } 
      gdt.onTouchEvent(event); 
      return true; 
     } 
    }; 
} 

添加監聽器訪問量:

private void addTouchListenerToView(View v) { 
    v.setOnTouchListener(touchListener); 
} 

樣品手勢監聽器類:

private class GestureListener extends GestureDetector.SimpleOnGestureListener { 
    private final int SWIPE_MIN_DISTANCE = 153; 
    private final int SWIPE_THRESHOLD_VELOCITY = 20; 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
     { 
      return false; // Right to left 
     } 
     else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) //&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
     { 
      return false; // Left to right 
     } 

     if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE)// && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) 
     { 
      return false; // Bottom to top 
     } 
     else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE)// && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) 
     { 
      return false; // Top to bottom 
     } 

     return true; 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
          float distanceY) { 
     if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE*10) //&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
     { 
      return false; // Right to left 
     } 
     else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE*10) //&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
     { 
      return false; // Left to right 
     } 
     return true; 
    } 
}