Vista web de Android: detección de desplazamiento

Necesito saber cómo detectar si el usuario no puede desplazarse más en un webView. Quiero generar una acción cuando el usuario desliza hacia la izquierda o hacia la derecha, pero sólo si el usuario no puede desplazarse:

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

He encontrado una solución que funciona para mí, comprobar el código fuente de la clase WebView en 2.3 API y encontrar cómo hacerlo con una API 2.1. Tal vez pueda funcionar con una API más antigua:

 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); } } 

Hay algunos métodos interesantes en la API, pero están protegidos, creo que puede crear su propio WebView (ampliar WebView) y utilizar esos métodos y hacerlos públicos. Específicamente, tal vez usted puede usar estos dos onScrollChanged y onOverScroll .

Creo que esto resolverá este problema:

 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); 
  • Reemplazo de Android Mapview para utilizar un servicio WMS
  • Escuchar Webview Key Events desde el teclado del software en la actividad de Android
  • Cómo obtener Bitmap de WebView genera un fallo de OutOfMemory
  • Necesita ayuda con el botón de retroactiva para volver a la vista web
  • Android 4.0.1 rompe el almacenamiento local HTML 5 de WebView?
  • ¿Por qué no puedo establecer currentTime y la duración es igual a 0 en HTML AudioElement en Android WebView?
  • Vídeo con iframe no se muestra en la vista web de Android
  • Política de seguridad de contenido de WebView
  • Android - Descargar e instalar el archivo apk en Webview
  • Desactivar ventanas emergentes y alarmboxes en la vista web de android
  • Mostrar la versión en caché de webview en android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.