Search code examples
androidscrollviewonfling

Scroll view and OnGestureListener


Here is the layout -

 <ScrollView>

<RelativeLayout>

    <Layout1>
    </Layout1>

    <Layout2>
    </Layout2>

    <Layout3>
    </Layout3>

    <Layout4>
    </Layout4>

    <Layout5>
    </Layout5>

    <Layout6>
    </Layout6>
</RelativeLayout>
</ScrollView>

I have implemented the onGestureListener in the Activity .
I have to detect the swipe actions- swipe up and swipe down on the individual layouts (layout 1 to layout 6). Since the layouts are in a scrollview the swipe action(OnFling) is not getting detected.
How to detect the onFling for the child elements of the scrollview ?
Any help will be highly appreciated.

Edit : Adding some code -

Here is the activity -

  public class ServiceScreen extends Activity implements OnFocusChangeListener,
    OnTouchListener, OnGestureListener ....

The layout and the listener -

RelativeLayout rlCarBrand;
    ....

    rlCarBrand.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return gestureScanner.onTouchEvent(event);
        }
    });

Overriden methods -

@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    Log.i("LogMessage", "On Fling");
    return true;
}


@Override
public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return true;
}

Solution

  • Create a CustomScrollView class that extends ScrollView.

    With this we can change the scrolling enabled property of the Scrollview.

    public class CustomScrollView extends ScrollView {
    
        boolean bScrollable = false;
    
        public CustomScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
    
        public void setScrollingEnabled(boolean enabled) {
            bScrollable = enabled;
        }
    
        public boolean isScrollable() {
            return bScrollable;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if we can scroll pass the event to the superclass
                if (bScrollable) {
                    return super.onTouchEvent(ev);
                }
    
                // only continue to handle the touch event if scrolling enabled
                return bScrollable; // mScrollable is always false at this point
    
            default:
                return super.onTouchEvent(ev);
            }
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            // Don't do anything with intercepted touch events if
            // we are not scrollable
            if (!bScrollable) {
                return false;
            } else {
                return super.onInterceptTouchEvent(ev);
            }
        }
    
    }