Using something similar to the answer of this question, I've tried to disable the ViewPager swipe action for when the user is swiping over a particular item. The view in question is a scrollable chart from the MPAndroidChart library, so naturally I don't want the view pager interfering with the scrolling of the chart.
The issue I am having is that the ViewPager will often have "onInterceptTouch" invoked before the onTouchListener is invoked on my desired view.
In this segment of code, I'm recording when the view is pressed/unpressed:
private long lastDown;
private long lastUp;
...
public void foo(){
barChart.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("AAA");
if(event.getAction() == MotionEvent.ACTION_DOWN){
lastDown = System.currentTimeMillis();
}else if(event.getAction() == MotionEvent.ACTION_UP){
lastUp = System.currentTimeMillis();
}
return false;
}
});
}
In this segment of code, I determine if the view is selected:
public boolean isGraphTouched(){
return lastDown > lastUp;
}
And in this segment of code I'm overriding the onInterceptTouchEvent method:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
System.out.println("BBB");
return isGraphSelected() ? super.onInterceptTouchEvent(ev) : false;
}
And if you take note of the printlines, the onInterceptTouchEvent method is called before...
The only way I can think of getting around this is to make a method which checks if the graph exists at the coordinates of the motion event (although I'm not sure if this is even possible) and then use that to determine if the pager will be swipable or not.
I managed to make it work using the function parent.requestDisallowInterceptTouchEvent(true); being placed inside the child view onTouchEvent(). This way the View does not allow none of his parents to interecpt his touch events in case a scroll happened and was to be handled by the ViewPager.
However in my case I had a ViewPager with a draggable custom views inside it which I wanted to move without that the ViewPager changes page.
My solution in terms of code: (Kotlin)
view.setOnTouchListener { v, event ->
parent.requestDisallowInterceptTouchEvent(true);
//Drag and drop handling is here and the rest of the event logic
}
I hope this will help you as well.
I've managed to solve my problem by incorporating this answer from another post. The code allows you to get the coordinates of a given view and compare them to your own coordinates.
Inside of my CustomViewPager class, I implemented what I mentioned above:
private boolean isPointInsideView(float x, float y, View view) {
int location[] = new int[2];
view.getLocationOnScreen(location);
int viewX = location[0];
int viewY = location[1];
return ((x > viewX && x < (viewX + view.getWidth())) && (y > viewY && y < (viewY + view.getHeight())));
}
I then had a method which returns a boolean, which checks a couple of conditions and returns true/false on if the pager should be able to be swiped:
public boolean canSwipe(float x, float y) {
boolean canSwipe = true;
if (launchActivity.isReady(MainScreenPagerAdapter.STAT_PAGE)) {
FragmentStatistics fragmentStatistics = (FragmentStatistics) launchActivity.getPageAdapter().instantiateItem(this, MainScreenPagerAdapter.STAT_PAGE);
View chart = fragmentStatistics.getView().findViewById(R.id.chart);
canSwipe = !isPointInsideView(x, y, chart) || !fragmentStatistics.isGraphPannable();
}
return canSwipe;
}
And then, of course, I overwrote the onInterceptTouchEvent like so:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return canSwipe(ev.getX(), ev.getY()) ? super.onInterceptTouchEvent(ev) : false;
}
And now, the graph can be fully panned without the Pager interfering with it at all.
Full CustomViewPager code:
public class CustomViewPager extends ViewPager {
/** Reference to the launch activity */
private LaunchActivity launchActivity;
/**
* Constructor to call the super constructor
*
* #param context The application context
* #param attrs The attributes
*/
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Sets object reference for the {#code launchActivity}
*
* #param launchActivity The LaunchActivity to be set
*/
public void set(LaunchActivity launchActivity) {
this.launchActivity = launchActivity;
}
/**
* Determines if the pager can be swiped based off the x and y inputs provided, as well as if the
* barchart can be panned or not.
*
* #param x The x coordinate to check
* #param y The y coordinate to check
* #return True if the ViewPager will continue with normal swiping action.
*/
public boolean canSwipe(float x, float y) {
boolean canSwipe = true;
if (launchActivity.isReady(MainScreenPagerAdapter.STAT_PAGE)) {
FragmentStatistics fragmentStatistics = (FragmentStatistics) launchActivity.getPageAdapter().instantiateItem(this, MainScreenPagerAdapter.STAT_PAGE);
View chart = fragmentStatistics.getView().findViewById(R.id.chart);
canSwipe = !isPointInsideView(x, y, chart) || !fragmentStatistics.isGraphPannable();
}
return canSwipe;
}
/**
* Takes x and y coordinates and compares them to the coordinates of the passed view. Returns true if the passed coordinates
* are within the range of the {#code view}
*
* #param x The x coordinate to compare
* #param y The y coordinate to compare
* #param view The view to check the coordinates of
* #return True if the x and y coordinates match that of the view
*/
private boolean isPointInsideView(float x, float y, View view) {
int location[] = new int[2];
view.getLocationOnScreen(location);
int viewX = location[0];
int viewY = location[1];
// point is inside view bounds
return ((x > viewX && x < (viewX + view.getWidth())) && (y > viewY && y < (viewY + view.getHeight())));
}
/**
* Override of the onInterceptTouchEvent which allows swiping to be disabled when chart is selected
*
* #param ev The MotionEvent object
* #return Call to super if true, otherwise returns false
*/
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return canSwipe(ev.getX(), ev.getY()) ? super.onInterceptTouchEvent(ev) : false;
}
}
Related
I'm writing a html page reader, cant load all .html because its huge and performance is bad, so I decided to split it into 3(or more) html and load that inside Web View nested with View Pager (swipe vertically)
My problem is that I should swipe slowly to scroll the web view and swipe fast to change view pager
Slow swiping
fast swipe change the Page on ViewPager
1) Can i expand webview to all its content heigh inside viewpager ?
2) Can change viewpager item only when topScroll or endScroll?
what i've tried so far:
CustomWebView
#Override
public boolean onTouchEvent(MotionEvent event) {
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
worked but was unable to change viewpager item
so i though about enable/disable it when i get to the top or to the end
removed webview onTouchEvent and added:
#Override
protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
requestDisallowInterceptTouchEvent(true);
int height = (int) Math.floor(this.getContentHeight() * this.getScale());
int webViewHeight = this.getMeasuredHeight();
boolean scrollTop = this.getTop() == t;
boolean scrollEnd = this.getScrollY() + webViewHeight >= height;
if(scrollTop || scrollEnd) {
requestDisallowInterceptTouchEvent(false);
}
worked randomly, the most common thing is that when i change page, i must scroll down and scroll up to trigger the requestDisallow to false so i can change page =[
Vertical CustomViewPage is this one ->
public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
super(context);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// The majority of the magic happens here
setPageTransformer(true, new VerticalPageTransformer());
// The easiest way to get rid of the overscroll drawing that happens on the left and right
setOverScrollMode(OVER_SCROLL_NEVER);
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
#Override
public void transformPage(View view, float position) {
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
//set Y position to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
/**
* Swaps the X and Y coordinates of your touch event.
*/
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();
float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;
ev.setLocation(newX, newY);
return ev;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}
}
1st i think it's a bad idea to split your content into 3 different HTML files and putting them on 3 different WebViews.
3 fils can be a good idea but 3 WebView in 3 Views of ViewPager is a bad idea.
Instead, you should detect top and bottom of the webpage using ".js" / "jQuery" and route page/HTML within same WebView.
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// you're at the bottom of the page
}};
There are many ways to detect TOP and BOTTOM of your HTML content.
Still, if you want to increase the height of your review to content height use
Getting WebView Content height once its loaded Android it worked for me.
The sample application (shown below with my additions) is included in the Almeros library. It allows the user to perform multi-touch gestures to move, size, rotate, and shove an object on an unchanging background:
I would like to implement a long click listener, but doing the obvious (adding implements View.OnLongClickListener and overriding public boolean onLongClick(View v) does not work (the method is never called).
What I have attempted to do is change the earth to a basketball through a long-click (both images are already included in the sample on github).
My question: How could long click events be used while preserving the existing gesture functionality provided by the library?
This is the sample code with a few of my few additions (which have been tagged with 20160326).
/**
* Test activity for testing the different GestureDetectors.
*
* #author Almer Thie (code.almeros.com)
* Copyright (c) 2013, Almer Thie (code.almeros.com)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
// 20170326 - Commented 1, Added 1
//public class TouchActivity extends Activity implements OnTouchListener {
public class TouchActivity extends AppCompatActivity implements View.OnTouchListener, View.OnLongClickListener {
private Matrix mMatrix = new Matrix();
private float mScaleFactor = .4f;
private float mRotationDegrees = 0.f;
private float mFocusX = 0.f;
private float mFocusY = 0.f;
private int mAlpha = 255;
private int mImageHeight, mImageWidth;
private ScaleGestureDetector mScaleDetector;
private RotateGestureDetector mRotateDetector;
private MoveGestureDetector mMoveDetector;
private ShoveGestureDetector mShoveDetector;
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Determine the center of the screen to center 'earth'
Display display = getWindowManager().getDefaultDisplay();
mFocusX = display.getWidth()/2f;
mFocusY = display.getHeight()/2f;
// Set this class as touchListener to the ImageView
ImageView view = (ImageView) findViewById(R.id.imageToMove);
view.setOnTouchListener(this);
//20160326 - Added 1
view.setOnLongClickListener(this);
// Determine dimensions of 'earth' image
Drawable d = this.getResources().getDrawable(R.drawable.earth);
mImageHeight = d.getIntrinsicHeight();
mImageWidth = d.getIntrinsicWidth();
// View is scaled and translated by matrix, so scale and translate initially
float scaledImageCenterX = (mImageWidth*mScaleFactor)/2;
float scaledImageCenterY = (mImageHeight*mScaleFactor)/2;
mMatrix.postScale(mScaleFactor, mScaleFactor);
mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);
view.setImageMatrix(mMatrix);
// Setup Gesture Detectors
mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());
mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener());
mMoveDetector = new MoveGestureDetector(getApplicationContext(), new MoveListener());
mShoveDetector = new ShoveGestureDetector(getApplicationContext(), new ShoveListener());
}
#SuppressWarnings("deprecation")
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
mRotateDetector.onTouchEvent(event);
mMoveDetector.onTouchEvent(event);
mShoveDetector.onTouchEvent(event);
float scaledImageCenterX = (mImageWidth*mScaleFactor)/2;
float scaledImageCenterY = (mImageHeight*mScaleFactor)/2;
mMatrix.reset();
mMatrix.postScale(mScaleFactor, mScaleFactor);
mMatrix.postRotate(mRotationDegrees, scaledImageCenterX, scaledImageCenterY);
mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);
ImageView view = (ImageView) v;
view.setImageMatrix(mMatrix);
view.setAlpha(mAlpha);
return true; // indicate event was handled
}
// 20170326 - Added method
#Override
public boolean onLongClick(View v) {
Log.v("20170326", "onLongClick called <<<<<<<<<<<<<<<<<<<<<<<<<<<");
ImageView view = (ImageView) findViewById(R.id.imageToMove);
view.setImageDrawable(this.getResources().getDrawable(R.drawable.basketball));
return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor(); // scale change since previous event
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
return true;
}
}
private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
#Override
public boolean onRotate(RotateGestureDetector detector) {
mRotationDegrees -= detector.getRotationDegreesDelta();
return true;
}
}
private class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
#Override
public boolean onMove(MoveGestureDetector detector) {
PointF d = detector.getFocusDelta();
mFocusX += d.x;
mFocusY += d.y;
// mFocusX = detector.getFocusX();
// mFocusY = detector.getFocusY();
return true;
}
}
private class ShoveListener extends ShoveGestureDetector.SimpleOnShoveGestureListener {
#Override
public boolean onShove(ShoveGestureDetector detector) {
mAlpha += detector.getShovePixelsDelta();
if (mAlpha > 255)
mAlpha = 255;
else if (mAlpha < 0)
mAlpha = 0;
return true;
}
}
}
As indicated by user mudit pant, having both Touch and Click listeners are a problem. So the approach should be to start again with the original sample code, and implement a LongPressListener private class in the same way that other listeners have been implemented:
// 20160327 - Added class
private class LongPressListener extends GestureDetector.SimpleOnGestureListener {
#Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
ImageView mView = (ImageView) findViewById(R.id.imageView);
mView.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.basketball));
}
}
Then, in the same way the other detectors were integrated in the onTouch() method, add the new listener:
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
mRotateDetector.onTouchEvent(event);
mShoveDetector.onTouchEvent(event);
mMoveDetector.onTouchEvent(event);
// 20160327 - Added 1
mLongPressDetector.onTouchEvent(event);
.
.
.
}
And similarly, in onCreate() create another detector:
public void onCreate(Bundle savedInstanceState) {
.
.
.
// Setup Gesture Detectors
.
.
.
// 20170327 - Added 1
mLongPressDetector = new GestureDetector(getApplicationContext(), new LongPressListener());
.
.
.
}
Upon long press, the image changes to the basketball:
The problem is that you are having both Touch and clicklisteners. The touch listener is called as soon as you touch the device and since you are returning true from there, this event is not propogated to the click listener. Hence your code is not executing. The best way for you to handle this is starting a timer on touch down to mimick the long press behaviour. You should cancel this timer in touch up / move events. If your timer expires fire the long click event work. Hope this helps. :)
The answer here suffers from triggering unwanted long press events. In other words, if the user takes more than GestureDetector's LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout(), the long press event is fired, even though the user might be just trying to move or rotate or size. Quick moves work fine, it's when they're trying to get precise, the long press event intercedes. My first thought was to increase the timeout, but it appears that it's system wide for all views, set at 500ms, and is not customizable.
Thus, an alternate solution which is very simple and does not need or use GestureDetector is to use Handler.postDelayed() along with a Runnable.
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
mRotateDetector.onTouchEvent(event);
mMoveDetector.onTouchEvent(event);
mShoveDetector.onTouchEvent(event);
// 20170329 - removed 1, added 'if' block
//mLongPressDetector.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Start a timer on each ACTION_DOWN
handler.postDelayed(mLongPressed, 1000);
}
.
.
.
}
In each of the sample code onTouch() methods, the timer is removed. In all cases except onMove(), the callback is removed unconditionally. But in the case of the MoveListener, it gets triggered even when there is zero offset. So I'll paste only the altered MoveListener here, but you need to put handler.removeCallbacks(mLongPressed); in all the listeners:
private class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
#Override
public boolean onMove(MoveGestureDetector detector) {
PointF d = detector.getFocusDelta();
mFocusX += d.x;
mFocusY += d.y;
// 20170329 - Added 'if' block - remove listener if there is non-zero offset
if ((d.x + d.y) != 0f) {
handler.removeCallbacks(mLongPressed);
Log.v(TAG, "onMove removed " + d.x + ", " + d.y);
}
return true;
}
}
Finally, in your activity, you instantiate the Handler and implement the Runnable
// 20170329 - Added Handler and Runnable
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
Log.v(TAG, "mLongPressed Runnable triggered.");
ImageView mView = (ImageView) findViewById(R.id.imageView);
mView.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.basketball));
}
};
This solution came from this MSquare answer.
Looking at the Drawable docs, we have a new method setHotspot (float x, float y) with the description of:
Specifies the hotspot's location within the drawable.
With no other explanations on that page, I wonder what the purpose is.
Hotspots are used to pipe touch events into RippleDrawable, but can be used by custom drawables as well. If you are implementing a custom View that manages its own drawables, you will need to call setHotspot() from the drawableHotspotChanged() method for touch-centered ripples to work correctly.
From View.java:
#Override
public boolean onTouchEvent(MotionEvent event) {
...
case MotionEvent.ACTION_MOVE:
drawableHotspotChanged(x, y);
...
}
/**
* This function is called whenever the view hotspot changes and needs to
* be propagated to drawables managed by the view.
* <p>
* Be sure to call through to the superclass when overriding this function.
*
* #param x hotspot x coordinate
* #param y hotspot y coordinate
*/
public void drawableHotspotChanged(float x, float y) {
if (mBackground != null) {
mBackground.setHotspot(x, y);
}
}
From FrameLayout.java, which manages its own mForeground drawable:
#Override
public void drawableHotspotChanged(float x, float y) {
super.drawableHotspotChanged(x, y);
if (mForeground != null) {
mForeground.setHotspot(x, y);
}
}
i'm using a SemiClosedSlidingDrawer (http://pastebin.com/FtVyrcEb) and i've added on content part some buttons on the top of slider which are always visibles.
The problems is that they are clickable (or click event is dispatched) only when slider is fully opened... When slider is "semi-opened" click event not seems dispached to button... I have inspected with debugger into onInterceptTouchEvent() and in both cases (opened/semi-collapsed) the following code
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (mLocked) {
return false;
}
final int action = event.getAction();
float x = event.getX();
float y = event.getY();
final Rect frame = mFrame;
final View handle = mHandle;
handle.getHitRect(frame);
//FOLLOWING THE CRITICAL CODE
if (!mTracking && !frame.contains((int) x, (int) y)) {
return false;
}
return false but only when slider is opened event was dispached...
It checks if a (x,y) relative to the click are contained in a rectangle created starting from the HandleButton view of sliding drawer...
final Rect frame = mFrame;
final View handle = mHandle;
handle.getHitRect(frame);
and this is obviously false because i'm clicking on a button contained inside the content part of slidingdrawer and that's ok...
As i said above the problem is that in semi-collapsed state, buttons contained in content part are not receiving the event...
Have you any idea how can i solve this issue?
Can be some state of slidingdrawer that avoid to click childs when collapsed?
Thanks in advance...
Right, I think I've figured out a way to do this.
First you need to modify onInterceptTouchEvent() to return true whenever the user presses the visible content during the semi-opened state. So, for instance, if your SemiClosedSlidingDrawer view is located at the very bottom of the screen, you can use a simple detection algorithm, like this:
public boolean onInterceptTouchEvent(MotionEvent event) {
...
handle.getHitRect(frame);
// NEW: Check if the user pressed on the "semi-open" content (below the handle):
if(!mTracking && (y >= frame.bottom) && action == MotionEvent.ACTION_DOWN) {
return true;
}
if (!mTracking && !frame.contains((int) x, (int) y)) {
...
}
Now the touch events during the user's interaction with the semi-opened content will be dispatched to onTouchEvent(). Now we just need to intercept these events and "manually" redirect them to the right view (note that we also need to offset the coordinates for the child view):
public boolean onTouchEvent(MotionEvent event) {
...
if (mTracking) {
...
}
else
{
// NEW: Dispatch events to the "semi-open" view:
final Rect frame = mFrame;
final View handle = mHandle;
handle.getHitRect(frame);
float x = event.getX();
float y = event.getY() - frame.bottom;
MotionEvent newEvent = MotionEvent.obtain(event);
newEvent.setLocation(x, y);
return mContent.dispatchTouchEvent(newEvent);
}
return mTracking || mAnimating || super.onTouchEvent(event);
}
It's a bit of a messy implementation, but I think the basic concept is right. Let me know how it works for you!
I am going to place text in gallery in android app. The text is in a scrollView. Everything works fine but on draging the text towards right or left, no next page/ element shows (The screen remains still).If i have some thing outside the scrollView, it changes to the next element on dragging.
Can anyone help?
You need to override onInterceptTouchEvent - you can use this to get the MotionEvents before they are delivered to the ScrollView, and then redirect them to your ViewGroup (Gallery in this case) if you wish.
The following class redirects MotionEvents to your Gallery if the user moves their finger too far left or right. Also if the user moves their finger up or down quite a bit then moving their finger left or right will have no longer have an effect, so you don't have to worry about the Gallery changing while doing a lot of scrolling.
class ScrollViewGallery extends Gallery {
/**
* The distance the user has to move their finger, in density independent
* pixels, before we count the motion as A) intended for the ScrollView if
* the motion is in the vertical direction or B) intended for ourselfs, if
* the motion is in the horizontal direction - after the user has moved this
* amount they are "locked" into this direction until the next ACTION_DOWN
* event
*/
private static final int DRAG_BOUNDS_IN_DP = 20;
/**
* A value representing the "unlocked" state - we test all MotionEvents
* when in this state to see whether a lock should be make
*/
private static final int SCROLL_LOCK_NONE = 0;
/**
* A value representing a lock in the vertical direction - once in this state
* we will never redirect MotionEvents from the ScrollView to ourself
*/
private static final int SCROLL_LOCK_VERTICAL = 1;
/**
* A value representing a lock in the horizontal direction - once in this
* state we will not deliver any more MotionEvents to the ScrollView, and
* will deliver them to ourselves instead.
*/
private static final int SCROLL_LOCK_HORIZONTAL = 2;
/**
* The drag bounds in density independent pixels converted to actual pixels
*/
private int mDragBoundsInPx = 0;
/**
* The coordinates of the intercepted ACTION_DOWN event
*/
private float mTouchStartX;
private float mTouchStartY;
/**
* The current scroll lock state
*/
private int mScrollLock = SCROLL_LOCK_NONE;
public ScrollViewGallery(Context context) {
super(context);
initCustomGallery(context);
}
public ScrollViewGallery(Context context, AttributeSet attrs) {
super(context, attrs);
initCustomGallery(context);
}
public ScrollViewGallery(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initCustomGallery(context);
}
private void initCustomGallery(Context context) {
final float scale = context.getResources().getDisplayMetrics().density;
mDragBoundsInPx = (int) (scale*DRAG_BOUNDS_IN_DP + 0.5f);
}
/**
* This will be called before the intercepted views onTouchEvent is called
* Return false to keep intercepting and passing the event on to the target view
* Return true and the target view will recieve ACTION_CANCEL, and the rest of the
* events will be delivered to our onTouchEvent
*/
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mTouchStartX = ev.getX();
mTouchStartY = ev.getY();
mScrollLock = SCROLL_LOCK_NONE;
/**
* Deliver the down event to the Gallery to avoid jerky scrolling
* if we decide to redirect the ScrollView events to ourself
*/
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
if (mScrollLock == SCROLL_LOCK_VERTICAL) {
// keep returning false to pass the events
// onto the ScrollView
return false;
}
final float touchDistanceX = (ev.getX() - mTouchStartX);
final float touchDistanceY = (ev.getY() - mTouchStartY);
if (Math.abs(touchDistanceY) > mDragBoundsInPx) {
mScrollLock = SCROLL_LOCK_VERTICAL;
return false;
}
if (Math.abs(touchDistanceX) > mDragBoundsInPx) {
mScrollLock = SCROLL_LOCK_HORIZONTAL; // gallery action
return true; // redirect MotionEvents to ourself
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// if we're still intercepting at this stage, make sure the gallery
// also recieves the up/cancel event as we gave it the down event earlier
super.onTouchEvent(ev);
break;
}
return false;
}
}
I understand your question. This situation is already handle by me after overriding method. I am not sure this will be the best solution, may be some one have more efficient solution then this, But it works for me.
I used customised object of gallery with the over ridden method.
public boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return super.onFling(e1, e2, 0, velocityY);
}
I found that if you place a textview in a vertical scrollbar mode it only moves up or down. If we want to swipe horizontally, you have to touch outside of the textview.