2013-07-27 14 views
6

我工作的手勢活動在android系統我用類檢測刷卡動作的Android如何在LinearLayout中添加滑動手勢不onDown真正

public class ActivitySwipeDetector implements View.OnTouchListener { 

    static final String logTag = "ActivitySwipeDetector"; 
    private Activity activity; 
    static final int MIN_DISTANCE = 100; 
    private float downX, downY, upX, upY; 

    public ActivitySwipeDetector(Activity activity){ 
     this.activity = activity; 
    } 

    public void onRightToLeftSwipe(){ 
     Log.i(logTag, "RightToLeftSwipe!"); 
     Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onLeftToRightSwipe(){ 
     Log.i(logTag, "LeftToRightSwipe!"); 
     Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onTopToBottomSwipe(){ 
     Log.i(logTag, "onTopToBottomSwipe!"); 
     //Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onBottomToTopSwipe(){ 
     Log.i(logTag, "onBottomToTopSwipe!"); 
     //Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     switch(event.getAction()){ 
     case MotionEvent.ACTION_DOWN: { 
      downX = event.getX(); 
      downY = event.getY(); 
      return true; 
     } 
     case MotionEvent.ACTION_UP: { 
      upX = event.getX(); 
      upY = event.getY(); 

      float deltaX = downX - upX; 
      float deltaY = downY - upY; 

      // swipe horizontal? 
        if(Math.abs(deltaX) > MIN_DISTANCE){ 
         // left or right 
         if(deltaX < 0) { this.onLeftToRightSwipe(); return true; } 
         if(deltaX > 0) { this.onRightToLeftSwipe(); return true; } 
        } 
        else { 
         Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); 
         return false; // We don't consume the event 
        } 

        // swipe vertical? 
        if(Math.abs(deltaY) > MIN_DISTANCE){ 
         // top or down 
         if(deltaY < 0) { this.onTopToBottomSwipe(); return true; } 
         if(deltaY > 0) { this.onBottomToTopSwipe(); return true; } 
        } 
        else { 
         Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); 
         return false; // We don't consume the event 
        } 

        return true; 
     } 
     } 
     return false; 
    } 

} 

我的XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/action_settings" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</LinearLayout> 

而在我的活動我打電話這樣

ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this); 
     LinearLayout lowestLayout = (LinearLayout)this.findViewById(R.id.action_settings); 
     lowestLayout.setOnTouchListener(activitySwipeDetector); 

我叫ActivitySwipeDetector(this);所以它的做工精細用滿滿一ctivity。但是,我怎樣才能將滑動檢測器僅應用於LinearLayout而不是整個活動。請幫助我。

+0

您正在爲特定視圖附加觸摸偵聽器。確定要檢測的滑動視圖,然後將其附加到適當的視圖。如果您將其附加到活動中的頂級視圖,就像您在此處所做的那樣,它將適用於整個活動。 –

+0

但是,如果我附加孩子查看它不工作... –

回答

18

所需對我來說,最好的是你的代碼,所以我定了一下,需要更多的修正,看到評論

public class RelativeLayoutTouchListener implements OnTouchListener { 

    static final String logTag = "ActivitySwipeDetector"; 
    private Activity activity; 
    static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance 
    private float downX, downY, upX, upY; 

    // private MainActivity mMainActivity; 

    public RelativeLayoutTouchListener(MainActivity mainActivity) { 
     activity = mainActivity; 
    } 

    public void onRightToLeftSwipe() { 
     Log.i(logTag, "RightToLeftSwipe!"); 
     Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onLeftToRightSwipe() { 
     Log.i(logTag, "LeftToRightSwipe!"); 
     Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onTopToBottomSwipe() { 
     Log.i(logTag, "onTopToBottomSwipe!"); 
     Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onBottomToTopSwipe() { 
     Log.i(logTag, "onBottomToTopSwipe!"); 
     Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: { 
      downX = event.getX(); 
      downY = event.getY(); 
      return true; 
     } 
     case MotionEvent.ACTION_UP: { 
      upX = event.getX(); 
      upY = event.getY(); 

      float deltaX = downX - upX; 
      float deltaY = downY - upY; 

      // swipe horizontal? 
      if (Math.abs(deltaX) > MIN_DISTANCE) { 
       // left or right 
       if (deltaX < 0) { 
        this.onLeftToRightSwipe(); 
        return true; 
       } 
       if (deltaX > 0) { 
        this.onRightToLeftSwipe(); 
        return true; 
       } 
      } else { 
       Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE); 
       // return false; // We don't consume the event 
      } 

      // swipe vertical? 
      if (Math.abs(deltaY) > MIN_DISTANCE) { 
       // top or down 
       if (deltaY < 0) { 
        this.onTopToBottomSwipe(); 
        return true; 
       } 
       if (deltaY > 0) { 
        this.onBottomToTopSwipe(); 
        return true; 
       } 
      } else { 
       Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long vertically, need at least " + MIN_DISTANCE); 
       // return false; // We don't consume the event 
      } 

      return false; // no swipe horizontally and no swipe vertically 
     }// case MotionEvent.ACTION_UP: 
     } 
     return false; 
    } 

} 

用法:與原來的

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    rlTop = (RelativeLayout) findViewById(R.id.rlTop);  
    rlTop.setOnTouchListener(new RelativeLayoutTouchListener(this)); 
} 
+0

我沒有讓你,你準確地擊中了? –

+0

我剛剛編輯你的代碼,檢查一個..我認爲你的問題完成.. –

+0

+1它幫助我感謝:) –

0

我有很好的效果碼。但是,偵聽器需要添加到視圖中,而不是佈局。畢竟,我們正在擴展View.OnTouchListener。

+1

LinearLayout擴展ViewGroup,它擴展了視圖。我無法明白爲什麼無法將偵聽器添加到佈局中。 – mhenry