首页 文章

列表视图和项目选择/点击的自定义渲染器

提问于
浏览
0

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

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

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

1 回答

  • 0

    我遇到了类似的问题,这就是我修复它的方法 . 请查看我对this stackoverflow问题的回答 . 在这个答案中,在代码背后的代码中,您可以看到我如何使用TapGestureRecognizer . 希望能帮助到你 .

相关问题