2014-12-30 63 views
3

滾動創建OnSwipeTouchListener類:Android的刷卡左/右pagging和上/下的web視圖

public class OnSwipeTouchListener implements OnTouchListener { 
private GestureDetector gestureDetector; 
public OnSwipeTouchListener(Context c) { 
    gestureDetector = new GestureDetector(c, new GestureListener()); 
} 
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 = 100; 
    private static final int SWIPE_VELOCITY_THRESHOLD = 100; 
    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 
    // Determines the fling velocity and then fires the appropriate swipe event accordingly 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean result = false; 
     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 > 0) { 
         onSwipeRight(); 
        } else { 
         onSwipeLeft(); 
        } 
       } 
      } else { 
       if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffY > 0) { 
         onSwipeDown(); 
        } else { 
         onSwipeUp(); 
        } 
       } 
      } 
     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
     return result; 
    } 
} 
public void onSwipeRight() { 
} 
public void onSwipeLeft() { 
} 
public void onSwipeUp() { 
} 
public void onSwipeDown() { 
} 
} 

而在ItemActivity我有一個web視圖(mWebView)並投入從資產HTML文件。從那以後,我穿上它的觸摸監聽器:

mWebView.setOnTouchListener(new OnSwipeTouchListener(this) { 
     @Override 
     public void onSwipeDown() { 
     } 
     @Override 
     public void onSwipeUp() { 

     } 

我實現onSwipeLeft和onSwipeUp正確,加載其他HTML-S卻不在所選HTML工作刷卡向上和向下滑動,以向上和向下滾動,所以我有問題顯示完整的HTML。

如何實現SwipeDown和SwipeUp垂直滾動工作?

回答

0

我變成ItemActivity這個覆蓋:

mWebView.setOnTouchListener(new OnSwipeTouchListener(this) { 
     @Override 
     public void onSwipeDown() { 
      mWebView.pageUp(false); 
     } 
     @Override 
     public void onSwipeUp() { 
      mWebView.pageDown(false); 
     } 

和正常工作。也許,有人會有所幫助。

它的解決方案是好的,但不能完美工作,我需要一個這樣的其他解決方案。

相關問題