Search code examples
androidnavigationscrollviewviewflipper

Viewflipper with Scrollviews and a nav bar


I have a viewflipper which includes a number of layouts. Each layout has a scrollview as the root.

Below the viewflipper I have a horizontal scrollview which contains textviews which acts as a navigation bar.

My original issue was that the fling stopped working as soon as I added a scrollview but I added the following code:

    @Override
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return gestureDetector.onTouchEvent(ev);
} 

And the scrolling now works along with the fling.

The only issue I have now though is that the HSV nav bar now acts a little strange. Sometimes when I try to slide it across, to select new contents, the HSV springs back to where it was. Other times the HSV thinks I want to fling and moves to the next contents. This is of course dependant of the speed I try to scroll it.

I want the nav bar to work independently of the flipper.

I'm trying to understand the code to fix it myself but not getting anywhere.

I have the following in my onCreate:

gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (gestureDetector.onTouchEvent(event)) {
            return true;
        }
        return false;
    }};

I have the MyGestureDetector class:

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                flipper.setInAnimation(flip_in_from_right);
                flipper.setOutAnimation(flip_out_to_left);
                flipper.showNext();

            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                flipper.setInAnimation(flip_in_from_left);
                flipper.setOutAnimation(flip_out_to_right);
                flipper.showPrevious();

            }
        } catch (Exception e) {
            // nothing
        }

And then the override:

    @Override
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return gestureDetector.onTouchEvent(ev);
} 

Can anyone shed any light on this?


Solution

  • Where exactly is your HSV? Is it pinned to the bottom of the screen? If so, here's what I would do. Within your onFling event, check the Y position of the fling and make sure it's not within your HSV. If it is, then return false and you should be good to go.