我有一个包含3个片段的viewpager。在第二个片段中,我有一个按钮。目前,这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
}
}
答案 0 :(得分:2)
我重构了您的代码并完成了解决方案。
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
祝您生日快乐! :)