Last active
August 29, 2015 14:15
-
-
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.
This file contains 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
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