Recent Posts

RSS Feeds

Android ScrollView and GestureListeners

While working on Foggle, I came across an interesting but frustrating problem. Google searches turned up some other coders with the same problem, but no real concrete solution. The problem was having a view that wanted to respond to gestures, but was itself contained inside a ScrollView. The ScrollView like to "eat" the gestures, as it turns out. After much trial-and-error, the solution I came up with that worked for me was to extend ScrollView so I could catch it eating the gestures, and then pass them through to my child view under some circumstance. My simple ScrollView class is below (FoggleBrowser is the child vie, and in my case is always the child of this FoggleScroller class):

package twinfeats.foggle;




import android.content.Context;


import android.util.AttributeSet;


import android.view.MotionEvent;


import android.widget.ScrollView;




public class FoggleScroller extends ScrollView {




public FoggleScroller(Context context) {


super(context);


}




public FoggleScroller(Context context, AttributeSet attrs) {


super(context, attrs);


}




public FoggleScroller(Context context, AttributeSet attrs, int defStyle) {


super(context, attrs, defStyle);


}






@Override


public boolean onInterceptTouchEvent(MotionEvent ev) {


boolean rc = super.onInterceptTouchEvent(ev);


if (rc) {


FoggleBrowser b = (FoggleBrowser)getChildAt(0);


b.onTouchEvent(ev);


}


return rc;


}




@Override


public boolean onTouchEvent(MotionEvent ev) {


boolean rc = super.onTouchEvent(ev);


if (!rc) {


FoggleBrowser b = (FoggleBrowser)getChildAt(0);


b.onTouchEvent(ev);


}


return rc;


}




}

 

Permalink     No Comments



Post a Comment:
  • HTML Syntax: Allowed