【问题标题】:Custom renderer for listview and item selected/tapped列表视图和项目选择/点击的自定义渲染器
【发布时间】:2017-03-14 18:58:17
【问题描述】:

我正在 xamarin 表单项目中为 android 的自定义渲染器开发左右滑动。 我有一个实现方法“DispatchTouchEvent(MotionEvent touch)”的覆盖的类。当 MotionEventActions.Up 被提出时,我会做一些事情。 我已经在我的 ListView 的共享代码中实现了一个“ItemSelected”事件,但是当我在 listview 的一个元素上滑动时,它解释为一个点击,这会触发这个事件。

点击或滑动元素时如何区分?

当我在“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;
        }
    }
}

【问题讨论】:

    标签: listview events xamarin xamarin.android xamarin.forms


    【解决方案1】:

    我遇到了类似的问题,这就是我解决它的方法。请参阅我对this stackoverflow 问题的回答。在这个答案中,在代码背后的代码中,您可以看到我是如何使用 TapGestureRecognizer 的。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多