我有一个有3个片段的viewpager。在第二个片段中,我有一个按钮。目前,对于所有3个片段,正在向左和向右滑动。现在在第二个片段中,只有在点击按钮后,用户才能进入第3个屏幕。否则从第二屏幕到第三屏幕滑动应该被阻止。当用户进入第三屏幕时,他可以返回到先前的屏幕。我编写了customviewpager代码,但它不能阻止特定的滑动。请帮我解决这个问题。
CustomViewPager.cs
namespace swiping
{
public class CustomViewPager : ViewPager
{
private float initialXValue;
private SwipeDirection direction;
private int position = 0;
CustomViewPager myadapter;
bool isrightswipe, isleftswipe;
public bool ShouldAllowSwipe()
{
if (this.CurrentItem == 1 && isleftswipe == true)
{
return false;
}
return true;
}
public CustomViewPager(Context context, IAttributeSet attrs) : base(context, attrs)
{
//this.direction = SwipeDirection.all;
}
public override bool OnTouchEvent(MotionEvent e)
{
//base.OnTouchEvent(e);
//if (this.IsSwipeAllowed(e))
//{
// return base.OnTouchEvent(e);
//}
IsSwipeAllowed(e);
if (ShouldAllowSwipe())
{
base.OnTouchEvent(e);
return true;
}
return false;
}
public override bool OnInterceptTouchEvent(MotionEvent e)
{
if (this.IsSwipeAllowed(e))
{
return base.OnInterceptTouchEvent(e);
}
return false;
}
private bool IsSwipeAllowed(MotionEvent e)
{
//if (this.direction == SwipeDirection.all) return true;
//if (direction == SwipeDirection.none)//disable any swipe
//return false;
isrightswipe = false;
isleftswipe = false;
if (e.Action == MotionEventActions.Down)
{
initialXValue = e.GetX();
}
if (e.Action == MotionEventActions.Move)
{
try
{
float diffX = e.GetX() - initialXValue;
if (diffX > 0 && direction == SwipeDirection.right)
{
// swipe from left to right detected
//prev
isleftswipe = false;
isrightswipe = true;
return false;
}
else if (diffX < 0 && direction == SwipeDirection.left)
{
// swipe from right to left detected
//next
isleftswipe = true;
isrightswipe = false;
return false;
}
}
catch (Exception ex)
{
}
}
return true;
}
public void setAllowedSwipeDirection(SwipeDirection direction)
{
this.direction = direction;
}
}
public enum SwipeDirection
{
all, left, right, none
}
}
我重构了你的代码并完成了解决方案。
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
}
}
您需要使用控件并向其添加OnPageChangeListener。在自定义页面侦听器中,您可以通过禁用滑动来实现自己的逻辑。
如何使用它,你可以在我的github上看到:https://gist.github.com/IlyaLavrov97/4bf2fb11ea195a0bbbaa2276a1a6c586
快乐的编码! :)