locked
ListView Swipe left / right on an item RRS feed

  • Question

  • I want to be able to determine when the user has swipped left or right on an item in the listview, and take a different action based on they swipped.

    I haven't been able to figure out how to do this - is this possible with the current listview, or will I have to manually do it?

    Thursday, November 15, 2012 3:57 AM

Answers

  • Based on : Identify the left to right and right to left swipe and GestureRecognizer CrossSliding Event

    you can implement this in a ListView as follows:

    - replace your <ListView ...> with <local:MyListView ...>

    - add the following classes to provide Pointer orientation whilst on ListViewItems:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Foundation;
    using Windows.UI.Input;
    using Windows.UI.Xaml.Controls;
    
    namespace App1
    {
        public class MyListView : ListView
        {
            protected override Windows.UI.Xaml.DependencyObject GetContainerForItemOverride()
            {
                // Use the 'MyListViewItem' in order to obtain Pointer Position information
                return new MyListViewItem();
            }
        }
        public class MyListViewItem : ListViewItem
        {
            protected override void OnPointerPressed(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
            {
                Debug.WriteLine("OnPointerPressed:{0}", DateTime.Now);
                base.OnPointerPressed(e);
            }
            protected override void OnPointerReleased(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
            {
                Debug.WriteLine("OnPointerReleased:{0}", DateTime.Now);
                base.OnPointerReleased(e);
            }
            protected override void OnPointerMoved(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
            {
                // Use this info to determine swipe-X|Y orientation
                // Clamp on values within [PointerPressed-PointerReleased] interval
                // Best to drop left-right or right-left nullable bool? in a Singleton class or Static property value
                // which can then be reached by any other code for further use in your business case
                IList<PointerPoint> list = e.GetIntermediatePoints(this);
                Debug.WriteLine("OnPointerMoved:{0} {1}", DateTime.Now, e.GetIntermediatePoints(this).Count > 0 ? list[0].Position : new Point(0, 0));
                base.OnPointerMoved(e);
            }
        }
    }
    

    • Marked as answer by Aaron Xue Friday, November 23, 2012 11:57 AM
    Thursday, November 15, 2012 7:18 AM

All replies

  • Based on : Identify the left to right and right to left swipe and GestureRecognizer CrossSliding Event

    you can implement this in a ListView as follows:

    - replace your <ListView ...> with <local:MyListView ...>

    - add the following classes to provide Pointer orientation whilst on ListViewItems:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Foundation;
    using Windows.UI.Input;
    using Windows.UI.Xaml.Controls;
    
    namespace App1
    {
        public class MyListView : ListView
        {
            protected override Windows.UI.Xaml.DependencyObject GetContainerForItemOverride()
            {
                // Use the 'MyListViewItem' in order to obtain Pointer Position information
                return new MyListViewItem();
            }
        }
        public class MyListViewItem : ListViewItem
        {
            protected override void OnPointerPressed(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
            {
                Debug.WriteLine("OnPointerPressed:{0}", DateTime.Now);
                base.OnPointerPressed(e);
            }
            protected override void OnPointerReleased(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
            {
                Debug.WriteLine("OnPointerReleased:{0}", DateTime.Now);
                base.OnPointerReleased(e);
            }
            protected override void OnPointerMoved(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
            {
                // Use this info to determine swipe-X|Y orientation
                // Clamp on values within [PointerPressed-PointerReleased] interval
                // Best to drop left-right or right-left nullable bool? in a Singleton class or Static property value
                // which can then be reached by any other code for further use in your business case
                IList<PointerPoint> list = e.GetIntermediatePoints(this);
                Debug.WriteLine("OnPointerMoved:{0} {1}", DateTime.Now, e.GetIntermediatePoints(this).Count > 0 ? list[0].Position : new Point(0, 0));
                base.OnPointerMoved(e);
            }
        }
    }
    

    • Marked as answer by Aaron Xue Friday, November 23, 2012 11:57 AM
    Thursday, November 15, 2012 7:18 AM
  • Thank you ForInfo. This was a great help. :)
    Monday, April 8, 2013 11:12 PM