2013-12-12 102 views
1

我想向ScrollView添加滑動功能(從左到右),但不攔截所有的觸摸事件,以便子按鈕仍然是可點擊的。在ScrollView中實現左/右滑動

我添加觸摸監聽器,這樣滾動視圖:

this.getView().findViewById(R.id.scrollView1).setOnTouchListener(new OnSwipeTouchListener() { 
      public void onSwipeTop() { 
       Toast.makeText(DetailFragment.this.getActivity(), "top", Toast.LENGTH_SHORT).show(); 
      } 

      public void onSwipeRight() { 
       Toast.makeText(DetailFragment.this.getActivity(), "right", Toast.LENGTH_SHORT).show(); 
      } 

      public void onSwipeLeft() { 
       Toast.makeText(DetailFragment.this.getActivity(), "left", Toast.LENGTH_SHORT).show(); 
      } 

      public void onSwipeBottom() { 
       Toast.makeText(DetailFragment.this.getActivity(), "bottom", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

如果我讓它喜歡它,那麼我的聽衆觸摸從不調用。因此,我不得不繼承ScrollView並覆蓋onInterceptTouchEvent,使滾動視圖將決定它要截取其中的觸摸事件:

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    boolean intercept = false; 
    final int action = ev.getAction(); 
    ViewConfiguration vc = ViewConfiguration.get(this.getContext()); 
    int slop = vc.getScaledTouchSlop(); 
    switch (action & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_UP: { 
      float currX = ev.getX(); 
      if (Math.abs(currX - originalX) > slop) { 
       intercept = true; 
      } 
      break; 
     } 
     case MotionEvent.ACTION_DOWN: { 
      originalX = ev.getX(); 
      break; 
     } 
    } 

    return (super.onInterceptTouchEvent(ev) || intercept); 
} 

所以我的想法是攔截只刷卡左/右和所有其他事件應該留給兒童的意見。滾動視圖中的按鈕似乎工作,但我的聽衆永遠不會被調用。

我在XML視圖中看起來是這樣的:

<com.damluar.mobile.inbox.MyScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:visibility="visible" 
      android:id="@+id/detailLayout"> 
     <LinearLayout 
       android:id="@+id/detailButtonLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:orientation="horizontal" 
       android:background="@color/default_color"> 
     </LinearLayout> 

    </LinearLayout> 
</com.damluar.mobile.inbox.MyScrollView> 

回答

1

請檢查這一點,如果有用的標記它。 我已經更新了一下OnSwipeTouchListener以獲得onTouch的回調。

public class OnSwipeTouchListener implements OnTouchListener { 
private final GestureDetector gestureDetector = new GestureDetector(
     new GestureListener()); 

@Override 
public boolean onTouch(final View view, final MotionEvent motionEvent) { 
    return gestureDetector.onTouchEvent(motionEvent); 

} 

private final class GestureListener extends SimpleOnGestureListener { 

    private static final int SWIPE_THRESHOLD = 50; 
    private static final int SWIPE_VELOCITY_THRESHOLD = 50; 
    private static final int SWIPE_DISTANCE = 50; 


    private boolean isFlingCall = false; 

    @Override 
    public boolean onDown(MotionEvent e) { 
     // TODO Auto-generated method stub 
     isFlingCall = false; 
     return true; 
    } 



    @Override 
    public boolean onSingleTapUp(MotionEvent e){  
     if(!isFlingCall) { 
      onTouch(); 
      return false; 
     } 
     else { 
      return true; 
     } 

    } 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 
     try { 
      float diffY = e2.getY() - e1.getY(); 
      float diffX = e2.getX() - e1.getX(); 
      if (Math.abs(diffX) > Math.abs(diffY)) { 
       if (Math.abs(diffX) > SWIPE_THRESHOLD 
         && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > SWIPE_DISTANCE) { 
         isFlingCall = true; 
         onSwipeRight(); 
         return true; 
        } 
        else { 
         onSwipeLeft(); 
         return true; 
        } 
       } 
      } 

     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
     onTouch(); 
     return true; 
    } 
} 

public void onSwipeRight() { 
} 

public void onSwipeLeft() { 
} 
public void onTouch() { 
} 
} 

感謝

JRH

+0

正如我寫的,我的觸摸監聽器永遠不會被調用。所以你的聽衆在這方面不會更好。 – damluar