2011-01-10 10 views
7

मुझे पता होना चाहिए कि उपयोगकर्ता कैसे वेबव्यू में स्क्रॉल नहीं कर सकता है या नहीं। मैं उपयोगकर्ता को बाएं या दाएं स्वाइप जब एक कार्रवाई उत्पन्न करने के लिए चाहते हैं, लेकिन उपयोगकर्ता स्क्रॉल नहीं कर सकते हैं केवल यदि:एंड्रॉइड वेबव्यू: स्क्रॉल

IF the user swipes to the left AND the webview can't scroll to left THEN 
do something 
ELSE 
let the webview scroll 

उत्तर

11

मुझे एक समाधान मिला जो मेरे लिए काम करता है, मैं 2.3 एपीआई में क्लास वेबव्यू के स्रोत कोड की जांच करता हूं और 2.1 एपीआई के साथ इसे कैसे करना है। हो सकता है कि यह पुराने एपीआई के साथ काम कर सकते हैं:

public class CustomWebView extends WebView { 

    private float oldX; 

    // indicate if horizontal scrollbar can't go more to the left 
    private boolean overScrollLeft = false; 

    // indicate if horizontal scrollbar can't go more to the right 
    private boolean overScrollRight = false; 

    // indicate if horizontal scrollbar can't go more to the left OR right 
    private boolean isScrolling = false; 

    public CustomWebView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 


    public CustomWebView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 



    public CustomWebView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // width of the vertical scrollbar 
     int scrollBarWidth = getVerticalScrollbarWidth(); 

     // width of the view depending of you set in the layout 
     int viewWidth = computeHorizontalScrollExtent(); 

     // width of the webpage depending of the zoom 
     int innerWidth = computeHorizontalScrollRange(); 

     // position of the left side of the horizontal scrollbar 
     int scrollBarLeftPos = computeHorizontalScrollOffset(); 

     // position of the right side of the horizontal scrollbar, the width of scroll is the width of view minus the width of vertical scrollbar 
     int scrollBarRightPos = scrollBarLeftPos + viewWidth - scrollBarWidth; 

     // if left pos of scroll bar is 0 left over scrolling is true 
     if(scrollBarLeftPos == 0) { 
      overScrollLeft = true; 
     } else { 
      overScrollLeft = false; 
     } 

     // if right pos of scroll bar is superior to webpage width: right over scrolling is true 
     if(scrollBarRightPos >= innerWidth) { 
      overScrollRight = true; 
     } else { 
      overScrollRight = false; 
     } 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: // when user touch the screen 
      // if scrollbar is the most left or right 
      if(overScrollLeft || overScrollRight) { 
       isScrolling = false; 
      } else { 
       isScrolling = true; 
      } 
      oldX = event.getX(); 
      break; 

     case MotionEvent.ACTION_UP: // when user stop to touch the screen 
      // if scrollbar can't go more to the left OR right 
      // this allow to force the user to do another gesture when he reach a side 
      if(!isScrolling) { 
       if(event.getX() > oldX && overScrollLeft) { 
        // left action 
       } 

       if(event.getX() < oldX && overScrollRight) { 
        // right actio 
       } 
      } 

      break; 
     default: 
      break; 
     } 
     return super.onTouchEvent(event); 
    } 

} 
+0

यह काम नहीं कर रहा है जब सामग्री ज़ूम इन या आउट हो। कोई भी समाधान? – Chitrang

3

वहाँ एपीआई में कुछ रोचक तरीके हैं, लेकिन वे सुरक्षित हैं, मैं आपको लगता है अपना स्वयं का वेब व्यू (वेबव्यू बढ़ाएं) बना सकते हैं और उन तरीकों का उपयोग कर सकते हैं और उन्हें सार्वजनिक बना सकते हैं। विशेष रूप से, शायद आप इन दो onScrollChanged और onOverScroll का उपयोग कर सकते हैं।

+0

onOverScroll विधि Android 2.3 के लिए है, इसलिए मैं 2.3 के लिए वेबव्यू के स्रोत कोड मिलता है और शामिल होंगे यह मेरी परियोजना में है। – Carbodrache

+0

जानना अच्छा है! और शुभकामनाएं – mdelolmo

0

मुझे लगता है कि यह इस समस्या का समाधान होगा:

int horizontalOffset = computeHorizontalScrollOffset(); 
int contentWidth = (int) Math.floor(getContentWidth() * getScale()); 
int viewportWidth = getWidth(); 

// 1 just for random error 
boolean reachRightEnd = ((horizontalOffset + viewportWidth) >= (contentWidth - 1)); 
boolean reachLeftEnd = (horizontalOffset == 0); 
संबंधित मुद्दे