Skip to content

Instantly share code, notes, and snippets.

@Adam--
Last active August 29, 2015 14:15
Show Gist options
  • Save Adam--/7cf610a4fc7dd32a3ece to your computer and use it in GitHub Desktop.
Save Adam--/7cf610a4fc7dd32a3ece to your computer and use it in GitHub Desktop.
Xamarin Android ViewPager (Android.Support.V4.View) that allows for swiping between fragments even when a touch event lands initially on a child control.
using System;
using Android.Content;
using Android.Support.V4.View;
using Android.Util;
using Android.Views;
namespace InterceptingViewPagerExample.Droid
{
class InterceptingViewPager : ViewPager
{
private float _initialMotionX;
private readonly int _scrollTouchSlop;
public InterceptingViewPager(Context context, IAttributeSet attrs) : base(context, attrs)
{
var viewConfig = ViewConfiguration.Get(context);
_scrollTouchSlop = viewConfig.ScaledTouchSlop;
}
public override bool OnInterceptTouchEvent(MotionEvent ev)
{
var motionX = ev.GetX();
var motionChangeX = Math.Abs(_initialMotionX - motionX);
if (ev.Action == MotionEventActions.Down)
{
_initialMotionX = motionX;
}
if (ev.Action == MotionEventActions.Move && motionChangeX > _scrollTouchSlop)
{
return true;
}
return base.OnInterceptTouchEvent(ev);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment