简体   繁体   中英

Swipe list View Items in Xamarin Forms (Android)

I am working on android application and i want to create Renderer in xamarin forms for swipe the items of ListView. But i don't know how swipe will work. If any body have any idea please share with me Here is my code:-

 public class NativeCell : ViewCell
    {
    }

here is code of Renderer:-

[assembly: ExportRenderer(typeof(ViewCell), typeof(CustomViewCell))] namespace SwipeListItemRenderer.Droid.CustomControls {
    public class CustomViewCell : ViewCellRenderer
    {
        protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
        {
            var cell= base.GetCellCore(item, convertView, parent, context);
            cell.GenericMotion += Cell_GenericMotion;

            return cell;
        }

        private void Cell_GenericMotion(object sender, GenericMotionEventArgs e)
        {

        }
    } }

I have build the renderer for ViewCell of listview, I want to swipe item left to right that that i want to fire gesture event:-

 private void Cell_GenericMotion(object sender, GenericMotionEventArgs e)
        {

        }

It's possible make swipe using PanGesture.

I've create swipecomponent with swipe event using pangesture in PCL project.

Attach PanGesture event

PanGestureRecognizer panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += PanGesture_PanUpdated;
GestureRecognizers.Add(panGesture);

PanGesture_PanUpdated

private void PanGesture_PanUpdated(object sender, PanUpdatedEventArgs e)
{
    try
    {
        switch (e.StatusType)
        {
            case GestureStatus.Running:
                {
                    _gestureX = e.TotalX;
                    _gestureY = e.TotalY;
                }
                break;
            case GestureStatus.Completed:
                {
                    IsSwipe = true;
                    //Debug.WriteLine("{0}  {1}", _gestureX, _gestureY);
                    if (Math.Abs(_gestureX) > Math.Abs(_gestureY))
                    {
                        if (_gestureX > 0)
                        {
                            OnSwipeRight(null);
                        }
                        else
                        {
                            OnSwipeLeft(null);
                        }
                    }
                    else
                    {
                        if (_gestureY > 0)
                        {
                            OnSwipeDown(null);
                        }
                        else
                        {
                            OnSwipeUP(null);
                        }
                    }
                }
                break;
        }
    }
    catch (Exception ex)
    {
    }
}

Add custom Event

public event EventHandler SwipeUP;
protected void OnSwipeUP(EventArgs e)
{
    if (SwipeUP != null)
        SwipeUP(this, e);
}

public event EventHandler SwipeDown;
protected void OnSwipeDown(EventArgs e)
{
    if (SwipeDown != null)
        SwipeDown(this, e);
}

public event EventHandler SwipeRight;
protected void OnSwipeRight(EventArgs e)
{
    if (SwipeRight != null)
        SwipeRight(this, e);
}

public event EventHandler SwipeLeft;
protected void OnSwipeLeft(EventArgs e)
{
    if (SwipeLeft != null)
        SwipeLeft(this, e);
}

Swipe sample with souceCode in github https://github.com/act70255/ListViewSwipeGesture Btw Please update xamarin to 2.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM