Created
June 2, 2014 13:25
-
-
Save derekbrameyer/3f8ee0a424d06e97eddd to your computer and use it in GitHub Desktop.
InterceptingScrollView, an Android ScrollView to use with SlidingUpPanel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.doomonafireball.demo.android.widget; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.widget.ScrollView; | |
/** | |
* User: derek Date: 12/7/13 Time: 2:37 PM | |
* | |
* This ScrollView intercepts all gestures when enabled, except for events when it has already been scrolled to the | |
* top. | |
* | |
* It is designed to work with the SlidingUpPanel. | |
*/ | |
public class InterceptingScrollView extends ScrollView { | |
private boolean mIsIntercepting = false; | |
private float mCurrX = 0.0f; | |
private float mCurrY = 0.0f; | |
public InterceptingScrollView(Context context) { | |
super(context); | |
} | |
public InterceptingScrollView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public InterceptingScrollView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent event) { | |
super.onInterceptTouchEvent(event); | |
getParent().requestDisallowInterceptTouchEvent(mIsIntercepting); | |
final int action = event.getAction(); | |
if (action == MotionEvent.ACTION_DOWN) { | |
mCurrX = event.getX(); | |
mCurrY = event.getY(); | |
} | |
return super.onInterceptTouchEvent(event); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
final int action = event.getAction(); | |
if (action == MotionEvent.ACTION_MOVE) { | |
if ((event.getY() - mCurrY) > Math.abs(event.getX() - mCurrX) && (getScrollY() <= 0.0f) | |
&& mIsIntercepting) { | |
// User scrolled vertically | |
mIsIntercepting = false; | |
getParent().requestDisallowInterceptTouchEvent(false); | |
} | |
} | |
return super.onTouchEvent(event); | |
} | |
public void setIsIntercepting(boolean isIntercepting) { | |
mIsIntercepting = isIntercepting; | |
getParent().requestDisallowInterceptTouchEvent(isIntercepting); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Derek!
I am trying to use this custom scroll view with my SlidingUpPanel but I can`t get what I want. I will try to explain you what I need:
The thing is that i am adapting my layout for smaller screens, so I added a ScrollView in the second panel. It is something like Google Maps do. The problem is that, in bigger screens where scrollview is not needed, when you try to slide up or down the second panel, if you are touching the scrollView area, panel do not move...
Here u have a video where u can see the problem: https://www.dropbox.com/s/epk51xwud5rsw1d/SCR_20140804_201644.mp4
If you click on Factura (it is out of the scrollview) panel slide up perfectly. If you click below factura (it is the scrollview) panel do not move.
Would InterceptingScrollView.java fit my needs? Thanks very much,
Pablo López