1

How to show a response while gesture input is in progress?

I'm using swipe down gesture to reload my WebView.

Using this for gesture detection:How to detect swipe direction between left/right and up/down

Problem

SimpleGestureListener captures the result of user input only. It cannot be used to show a response, e.g. animation, while the user is performing a gesture.

Imperfect inelegant solution:

flaw: Shows animation independent of SimpleOnGestureListener; a response to user gesture input is displayed and gesture input may still fail or vice versa.

private  volatile  float userTouchY = -1; 
// translating WebView every onTouch event call is inefficient.
// only translate when translateYBy value is greater than a threshold.
private  float translateYBy = 0;
/**
 * Shift webView without animation.
 * @param num
 */
private void __shiftWebViewDownBy(int num){
    float current = webView.getY();
    if(current >= webviewTranslationLimit) return;
    current += num;
    if(current> webviewTranslationLimit) current = webviewTranslationLimit;
    webView.setY(current);
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    //Skip everything unless at the content top.
    if(webView.getScrollY() > 0) return false;
    int action = event.getAction();
    if(action == MotionEvent.ACTION_MOVE){
        float y = event.getY();
        float dif = y - userTouchY;
        if(dif < webviewTranslationLimit/10){
            //dif less than screenHeight*1/40, ignore input
        }else if(dif < -(webviewTranslationLimit/5)){
            //swipe up
            userTouchY = -1;//not swipe down cancelling
            __shiftWebViewTopTo(0);
        }else if(userTouchY < y) {
            //swipe down
            translateYBy += dif;
            if(userTouchY == -1){
                //userTouchY at the initial value, ignore for this once.
                userTouchY = y;
            }else{
                userTouchY = y;
                if(translateYBy > 5 ){
                    __shiftWebViewDownBy((int)translateYBy);
                    translateYBy = 0;
                }
            }
        }
    }else{
        Log.d(TAG,"action not move:" +MotionEvent.actionToString(action));
    // Webview shift down should only occur while moving
    // Once finger is off,cancel
        __shiftWebViewTopTo(0);
        userTouchY = -1;
    }
    boolean result = gestureDetector.onTouchEvent(event);
    return result;
}

Writing a GestureListener of a sort from scratch obviously solves the problem and I can simply use Toast to message the user for a failed gesture input, but there ought to be an easier, more readable solution.

Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45
TastyCatFood
  • 1,632
  • 1
  • 15
  • 27

0 Answers0