【问题标题】:Xamrin Forms : Swipe to delete(gesture) in ListViewXamarin Forms:在 ListView 中滑动以删除(手势)
【发布时间】:2014-09-17 09:30:59
【问题描述】:

我想在 Xamrin Forms 中实现滑动删除功能,为此我尝试了以下方法。

  1. 为列表视图编写了一个自定义渲染器,并且在渲染器的“OnElementChanged”中能够访问绑定到“CustomListView”的命令,并且能够将此命令添加到如下添加的滑动手势中。

       swipeGestureRecognizer = new UISwipeGestureRecognizer (() => {
            if (command == null) {
                Console.WriteLine ("No command set");
                return;}
    
            command.Execute (null);
        });
    

但是我在访问特定行(滑动行)时遇到问题,因此我可以在列表视图的滑动行上显示/隐藏按钮。请您推荐一种实现相同的方法吗?

【问题讨论】:

  • 我会将手势添加到 ItemTemplate 或 UITableCell。
  • 你是否也为 android 实现了这一点?

标签: xaml xamarin swipe-gesture xamarin.forms


【解决方案1】:

现在,使用 ContextAction 将滑动删除功能内置到 Xamarin Froms ListViews 中。这是如何做到这一点的最基本的教程。很容易实现。

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/listview/

【讨论】:

  • xamarin 表单实现确实涵盖了最基本的用例。仅供参考:不幸的是,您无法使用 xamarin 表单实现向右滑动。您还遇到了根本无法真正自定义的最基本菜单项(例如,为文本选择不同的字体),这意味着您几乎需要立即开始查看自定义渲染器。
  • 是的,我发现这方面的文档特别缺乏。
【解决方案2】:

你可以这样做:

    protected override void OnElementChanged (ElementChangedEventArgs<ListView> e)
    {
        base.OnElementChanged (e);
        var swipeDelegate = new SwipeRecogniserDelegate ();
        swipeGestureRecognizer = new UISwipeGestureRecognizer {
            Direction = UISwipeGestureRecognizerDirection.Left,
            Delegate = swipeDelegate
        };
        swipeGestureRecognizer.AddTarget (o => {
            var startPoint = swipeDelegate.GetStartPoint ();
            Console.WriteLine (startPoint);
            var indexPath = this.Control.IndexPathForRowAtPoint(startPoint);
            if(listView.SwipeCommand != null) {
                listView.SwipeCommand.Execute(indexPath.Row);
            }
        });
        this.Control.AddGestureRecognizer (swipeGestureRecognizer);
        this.listView = (SwipableListView)this.Element;
    }

密钥是SwipeRecogniserDelegate。它是这样实现的:

public class SwipeRecogniserDelegate : UIGestureRecognizerDelegate
{
    PointF startPoint;

    public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch)
    {
        return true;
    }

    public override bool ShouldBegin (UIGestureRecognizer recognizer)
    {
        var swipeGesture = ((UISwipeGestureRecognizer)recognizer);
        this.startPoint = swipeGesture.LocationOfTouch (0, swipeGesture.View);
        return true;
    }

    public PointF GetStartPoint ()
    {
        return startPoint;
    }
}

【讨论】:

    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多