我正在 xamarin 表单项目中为 android 的自定义渲染器开发左右滑动。我有一个实现方法“ DispatchTouchEvent(MotionEvent touch)”的重写的类。当引发 MotionEventActions.Up 时,我会做一些事情。我已经在我的 ListView 的共享代码中实现了一个“ItemSelected”事件,但是当我在列表视图的一个元素上滑动时,它会解释这是一个点击,这会触发这个事件。
在元素中点击或滑动时如何区分?
当我在“MotionEventActions.Up”中滑动时,我一直在思考差异化,但我不知道如何从自定义渲染器显示新页面。
这是渲染器的代码:
class SwipeListRenderer : ListViewRenderer
{
bool swipe = false;
bool scroll;
double puntoYinicial = 0.0;
public override bool DispatchTouchEvent(MotionEvent touch)
{
try
{
if (puntoYinicial == 0.0)
puntoYinicial = touch.GetY();
double currentQuota = ((touch.GetX() - TouchDispatcher.StartingBiasX) / (double)this.Width);
float x = touch.GetX();
float y = touch.GetY();
SwipeList.SwipeItemView touchedElement = (TouchDispatcher.TouchingView as SwipeList.SwipeItemView);
switch (touch.ActionMasked)
{
case MotionEventActions.Down:
(this.Element as SwipeList.SwipeListView).RestoreItemPosition();
swipe = false;
scroll = false;
return base.DispatchTouchEvent(touch);
break;
case MotionEventActions.Up:
if (swipe)
touchedElement.CompleteTranslation(currentQuota);
(this.Element as SwipeList.SwipeListView).AppendTouchedElement(touchedElement);
TouchDispatcher.TouchingView = null;
TouchDispatcher.StartingBiasX = 0;
TouchDispatcher.StartingBiasY = 0;
puntoYinicial = 0.0;
break;
case MotionEventActions.Move:
if (touchedElement != null)
TouchDispatcher.TouchingView.PerformTranslation(currentQuota);
break;
}
return base.DispatchTouchEvent(touch);
}
catch (Exception ex)
{
throw;
}
}
}
我遇到了类似的问题,这就是我解决它的方法。请参阅我对这个 stackoverflow问题的回答。在这个答案中,在代码背后的代码中,您可以看到我如何使用 TapGestureRecognizer。希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句