J'ai besoin de savoir comment détecter si l'utilisateur ne peut plus faire défiler dans un webview. Je souhaite générer une action lorsque l'utilisateur passe à gauche ou à droite, mais que si l'utilisateur ne peut pas faire défiler:
3 Réponses :
Il existe des méthodes intéressantes dans l'API, mais elles sont protégées, je pense que vous pouvez créer votre propre webview (étendre webview) et utiliser ces méthodes et les rendre publics. Plus précisément, vous pouvez peut-être utiliser ces deux Onscrollchanged et < un href = "http://developer.androïd.com/reference/andrroid/webkit/webview.html#onoverscrolled%28intInter%29" rel = "nofollow"> onoverscroll . p>
La méthode OnoverScroll est pour Android 2.3, donc je vais obtenir le code source de WebView pour 2.3 et l'inclure dans mon projet.
J'ai trouvé une solution qui fonctionne pour moi, je vérifie le code source de la classe webview en 2.3 API et trouvez comment le faire avec une API 2.1. Peut-être que cela peut fonctionner avec une API plus ancienne:
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); } }
Cela ne fonctionne pas lorsque le contenu est zoom avant ou arrière. Toute solution?
Je pense que cela résoudra ce problème:
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);