Skip to content

Instantly share code, notes, and snippets.

@IlyaLavrov97
Last active March 4, 2019 22:26
Show Gist options
  • Save IlyaLavrov97/4bf2fb11ea195a0bbbaa2276a1a6c586 to your computer and use it in GitHub Desktop.
Save IlyaLavrov97/4bf2fb11ea195a0bbbaa2276a1a6c586 to your computer and use it in GitHub Desktop.
using System;
using Android.Content;
using Android.Runtime;
using Android.Support.V4.View;
using Android.Util;
using Android.Views;
namespace Android.Base
{
[Register("ViewPagerWithCustomSwipe")]
public class ViewPagerWithCustomSwipe : ViewPager
{
private float InitialX;
private SwipeDirection DisabledSwipeDirection;
public ViewPagerWithCustomSwipe(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
protected ViewPagerWithCustomSwipe(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public ViewPagerWithCustomSwipe(Context context) : base(context)
{
}
public override bool OnTouchEvent(MotionEvent e)
{
if (IsSwipeAllowed(e))
return base.OnTouchEvent(e);
return false;
}
public override bool OnInterceptTouchEvent(MotionEvent e)
{
if (IsSwipeAllowed(e))
return base.OnInterceptTouchEvent(e);
return false;
}
private bool IsSwipeAllowed(MotionEvent e)
{
if (DisabledSwipeDirection == SwipeDirection.All)
return false;
if (DisabledSwipeDirection == SwipeDirection.None)
return true;
if (e.Action == MotionEventActions.Down)
InitialX = e.GetX();
if (e.Action == MotionEventActions.Move)
{
float diffX = e.GetX() - InitialX;
if (diffX > 0 && DisabledSwipeDirection == SwipeDirection.Right)
return false;
else if (diffX < 0 && DisabledSwipeDirection == SwipeDirection.Left)
return false;
}
return true;
}
public void DisableSwipe(SwipeDirection direction)
{
DisabledSwipeDirection = direction;
}
}
public enum SwipeDirection
{
All = 0,
Left = 1,
Right = 2,
None = 3
}
}
<ViewPagerWithCustomSwipe
android:id="@+id/viewpager"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class CustomPageChangeListener : Java.Lang.Object, ViewPager.IOnPageChangeListener
{
ViewPagerWithCustomSwipe _vp;
public CustomPageChangeListener(ViewPagerWithCustomSwipe vp)
{
_vp = vp;
}
public void OnPageScrollStateChanged(int state)
{
EnableDisableSwipeRefresh(state == ViewPager.ScrollStateIdle);
}
public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
public void OnPageSelected(int position)
{
// TODO Your custom selector
if(position == customPosition)
{
_vp.DisableSwipe(SwipeDirection.CustomDirection);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment