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
- Cómo depurar javascript en webview en android
- Comunicación con vistas Web integradas (UI) en aplicaciones iOS y Android existentes
- Ajuste el tamaño de fuente de Android WebView
- Forzar el no-flash Vimeo incrustar o deshabilitar Flash en un WebView?
- Múltiples WebViews en ScrollView dando problemas (Android)
- Vista web de Android, inicie la actividad ACTION_VIEW cuando la URL no pueda ser manejada por webview
- Pantalla Splash mientras carga una url en una vista web en la aplicación Android
- OnLongClickListener no funciona en WebView
- Acceso al código JS desde una aplicación nativa de Android
- Interceptar llamada de red desde Android Java / JNI
- Cómo activar la cámara frontal en la vista web de android
- Html de Webview Hacer clic en el botón de detección en la actividad (código java)
- Al seleccionar una imagen, la aplicación se reinicia en S3
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);
- Menú emergente de Android
- Los videos de incrustación de YouTube no funcionan en la vista web. ¿Qué tiene de malo este código?