d1vivek681065
d1vivek681065

Reputation: 181

Random scroll behavour on gestureDetector onScroll() method

I am implementing a scrolling panel in a app where user scroll the hidden container from top like as of Android's status bar notification panel.When i scroll top to bottom it works fine but when i scroll from bottom to top the MotionEvent e2 gives random value. Here's what i have done:

My touch listener :

upSlide.setOnTouchListener(bottomTouchListener);

GestureDetector:

GestureDetector bottomGestureDetector = new GestureDetector(
        context, new BottomTouchGestureListener());

//touch listener
OnTouchListener bottomTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                animateView(sliderView.getTop(), -screenHeight);
            return true;
        }
        return bottomGestureDetector.onTouchEvent(motionEvent);
    }
};

The GestureDetector:

class BottomTouchGestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if (e2.getAction() == MotionEvent.ACTION_MOVE) {
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    //left right swipe
                } else {
                    //top down swipe
                    slideOffset = Math.abs((int) diffY);
                    Log.e(TAG, "onScroll -->" + e2.getY() + "");
                    invalidate();
                }
            } catch (Exception exception) {
            }
        }
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return super.onSingleTapUp(e);
    }
}

And my log output is:

onScroll --> 40.83081
onScroll --> 1855.7668
onScroll --> 71.79602
onScroll --> 1843.0444
onScroll --> 50.157593
onScroll --> 1815.4504
onScroll --> 45.553223
onScroll --> 1778.2007
onScroll --> 48.40674
onScroll --> 1751.7765
onScroll --> 44.186157
onScroll --> 1715.3464
onScroll --> 44.40625
onScroll --> 1677.5885
onScroll --> 35.96411
onScroll --> 1624.1068
onScroll --> 22.843506
onScroll --> 1544.1412
onScroll --> 12.907837
onScroll --> 1441.6538
onScroll --> -5.8792725
onScroll --> 1300.1215
onScroll --> -36.662354
onScroll --> 1085.7295
onScroll --> -61.72827
onScroll --> 818.67615
onScroll --> -69.63501
onScroll --> 544.83466

If you notice the value for touch event MotionEvent e2are fluctuating onScroll so my view is also flickering. I dont understand the behaviour. Can someone explain why motionEvent e2 is that random and solution for this??? It is working when i scroll from top to bottom but not while i scroll from bottom to top.

Upvotes: 0

Views: 768

Answers (1)

Manuel
Manuel

Reputation: 15042

This is because when you hide the status bar (or any other view in the vertical structure), the vertical scroll coordinates are affected.

If you look at the onScroll docs, it says that distanceY is the delta between now and the last time onScroll was called. The last time onScroll was called in your case was before the status bar disappeared.

So the behavior is not "random" as you suggest, but rather a continuous toggle.

Upvotes: 1

Related Questions