2010-12-12 19:17:54 -05:00
|
|
|
package com.fsck.k9.view;
|
2010-07-13 17:49:28 -04:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.AttributeSet;
|
2011-01-19 12:05:56 -05:00
|
|
|
import android.view.GestureDetector;
|
2010-07-13 17:49:28 -04:00
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.widget.ScrollView;
|
|
|
|
|
|
|
|
public class ToggleScrollView extends ScrollView
|
|
|
|
{
|
2011-01-19 12:05:56 -05:00
|
|
|
private GestureDetector mDetector;
|
2010-07-13 17:49:28 -04:00
|
|
|
private boolean mScrolling = true;
|
|
|
|
|
|
|
|
public ToggleScrollView(Context context, AttributeSet attrs)
|
|
|
|
{
|
|
|
|
super(context, attrs);
|
2011-01-19 12:05:56 -05:00
|
|
|
mDetector = new GestureDetector(new YScrollDetector());
|
2010-07-13 17:49:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setScrolling(boolean enable)
|
|
|
|
{
|
|
|
|
mScrolling = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onTouchEvent(MotionEvent ev)
|
|
|
|
{
|
|
|
|
return (mScrolling) ? super.onTouchEvent(ev) : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onInterceptTouchEvent(MotionEvent ev)
|
|
|
|
{
|
2011-01-22 19:56:01 -05:00
|
|
|
if(!mScrolling)
|
|
|
|
{
|
2011-01-19 12:05:56 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This doesn't quite get us to diagonal scrolling, but it's somewhat better than what we've
|
|
|
|
// currently got. This is based on
|
|
|
|
// http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling
|
|
|
|
boolean result = super.onInterceptTouchEvent(ev);
|
2011-01-30 17:05:47 -05:00
|
|
|
// Let the original ScrollView handle ACTION_DOWN so we can stop the scroll when someone touches the screen.
|
|
|
|
if (ev.getAction() == MotionEvent.ACTION_DOWN || mDetector.onTouchEvent(ev))
|
2011-01-19 12:05:56 -05:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-30 17:05:47 -05:00
|
|
|
// Return false if we're scrolling in the x direction. That is, decline to consume the event and
|
|
|
|
// let the parent class take a stab at it.
|
2011-01-19 12:05:56 -05:00
|
|
|
class YScrollDetector extends GestureDetector.SimpleOnGestureListener
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (Math.abs(distanceY) > Math.abs(distanceX))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-22 19:56:01 -05:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
2011-01-19 12:05:56 -05:00
|
|
|
{
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-13 17:49:28 -04:00
|
|
|
}
|
|
|
|
}
|