Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created January 28, 2013 14:41
Show Gist options
  • Save Cheesebaron/4656031 to your computer and use it in GitHub Desktop.
Save Cheesebaron/4656031 to your computer and use it in GitHub Desktop.
Y locking ScrollView
using System;
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
namespace Cheesebaron.HorizontalListView
{
public class YScrollView : ScrollView
{
private float _xDistance, _yDistance, _lastX, _lastY;
protected YScrollView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public YScrollView(Context context) : base(context)
{
}
public YScrollView(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public YScrollView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
}
public override bool OnInterceptTouchEvent(MotionEvent ev)
{
switch (ev.Action) {
case MotionEventActions.Down:
_xDistance = _yDistance = 0f;
_lastX = ev.GetX();
_lastY = ev.GetY();
break;
case MotionEventActions.Move:
var curX = ev.GetX();
var curY = ev.GetY();
_xDistance += Math.Abs(curX - _lastX);
_yDistance += Math.Abs(curY - _lastY);
_lastX = curX;
_lastY = curY;
if(_xDistance > _yDistance)
return false;
break;
}
return base.OnInterceptTouchEvent(ev);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment