protected override void OnElementChanged(ElementChangedEventArgs <ListView> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                Control.Adapter?.Dispose();
                Control.Adapter = null;
                Control.SetOnDragListener(null);
                Control.OnItemLongClickListener = null;
            }

            if (e.NewElement != null)
            {
                _dragAndDropListViewAdapter = new DragAndDropListViewAdapter(Control, Element);
                Control.Adapter             = _dragAndDropListViewAdapter;
                Control.SetOnDragListener(_dragAndDropListViewAdapter);
                Control.OnItemLongClickListener = _dragAndDropListViewAdapter;
            }
        }
Example #2
0
        //public override bool HasStableIds => WrappedAdapter.HasStableIds;

        //public override bool IsEmpty => WrappedAdapter.IsEmpty;

        //public override int ViewTypeCount => WrappedAdapter.ViewTypeCount;

        //public override bool AreAllItemsEnabled() => WrappedAdapter.AreAllItemsEnabled();

        public bool OnDrag(View v, DragEvent e)
        {
            var dragItem = (DragItem)e.LocalState;

            switch (e.Action)
            {
            case DragAction.Started:
                break;

            case DragAction.Entered:
                System.Diagnostics.Debug.WriteLine($"DragAction.Entered from {v.GetType()}");

                if (!(v is ListView))
                {
                    //var dragItem = (DragItem)e.LocalState;

                    var targetPosition = InsertOntoView(v, dragItem);

                    dragItem.Index = targetPosition;

                    // Keep a list of items that has translation so we can reset
                    // them once the drag'n'drop is finished.
                    _translatedItems.Add(v);
                    _listView.Invalidate();
                }
                break;

            case DragAction.Location:

                var currentPosition = (int)e.GetX();

                var point = GetTouchPositionFromDragEvent(v, e);

                System.Diagnostics.Debug.WriteLine($"DragAction.Location from {v.GetType()} => {currentPosition}");
                System.Diagnostics.Debug.WriteLine($"DragAction.GLOBAL   from {v.GetType()} => {point.X}");

                int y           = Java.Lang.Math.Round(e.GetY());
                int translatedY = y - 50;
                int threshold   = 50;
                // make a scrolling up due the y has passed the threshold
                if (translatedY < threshold)
                {
                    // make a scroll up by 30 px
                    _hostScrollView.ScrollBy(0, -30);
                }
                // make a autoscrolling down due y has passed the 500 px border
                if (translatedY + threshold > 500)
                {
                    // make a scroll down by 30 px
                    _hostScrollView.ScrollBy(0, 30);
                }

                //if (point.X > App.AppScreenWidth - 50)
                //{
                //    //_hostScrollView.ScrollToAsync(_hostScrollView.ScrollX + 30, 0, true);
                //    _hostScrollView.StartScroll(50);
                //}

                //if (point.X < 50)
                //{
                //    //_hostScrollView.ScrollToAsync(_hostScrollView.ScrollX - 30, 0, true);
                //    _hostScrollView.StartScroll(-50);
                //}

                //if (currentPosition > _element.Width - 40)
                //{
                //    _hostScrollView.ScrollToAsync(_hostScrollView.ScrollX + 30, 0, true);
                //}
                //else if (currentPosition < 40)
                //{
                //    _hostScrollView.ScrollToAsync(_hostScrollView.ScrollX - 30, 0, true);
                //}

                //if (v.Right < (_hostScrollView.ScrollX + _hostScrollView.Width - 100))
                //{
                //    _hostScrollView.ScrollToAsync(_hostScrollView.ScrollX + 30, 0, true);
                //}

                //if (v.Left > (_hostScrollView.ScrollX + _hostScrollView.Width - 100))
                //{
                //    _hostScrollView.ScrollToAsync(_hostScrollView.ScrollX - 30, 0, true);
                //}

                break;

            case DragAction.Exited:
                System.Diagnostics.Debug.WriteLine($"DragAction.Entered from {v.GetType()}");

                _lastPosition = _listView.GetPositionForView(v);

                if (!(v is ListView))
                {
                    var positionEntered = GetListPositionForView(v);
                    //var item1 = _listAdapter.GetItem(positionEntered);

                    System.Diagnostics.Debug.WriteLine($"DragAction.Exited index {positionEntered}");
                }
                break;

            case DragAction.Drop:

                System.Diagnostics.Debug.WriteLine($"DragAction.Drop from {v.GetType()}");

                if (v is ListView)
                {
                    bool isDropped      = true;
                    int  positionSource = -1;
                    int  insertLocation = -1;

                    var dropItem                  = (DragItem)e.LocalState;
                    Android.Views.View view       = dropItem.View;
                    object             passedItem = dropItem.Item;

                    Android.Widget.ListView oldParent = (Android.Widget.ListView)view.Parent;

                    //DragListAdapter oldParentAdapter = (DragListAdapter)oldParent.;
                    BaseAdapter sourceAdapter = (oldParent.Adapter is IWrapperListAdapter)
                            ? ((IWrapperListAdapter)oldParent.Adapter).WrappedAdapter as BaseAdapter
                            : ((BaseAdapter)oldParent.Adapter);
                    DragAndDropListViewAdapter sourceDragListAdaptor = sourceAdapter as DragAndDropListViewAdapter;

                    //positionSource = (int)view.Tag;

                    Console.WriteLine(v.Parent.Parent);
                    Android.Widget.ListView newParent = _listView;

                    //Person customList = (Person)sourceAdapter.Collection[positionSource];
                    ObservableListCollection <Item> sourceList = sourceDragListAdaptor.Collection;
                    //customListSource.RemoveAt(positionSource);



                    //Android.Widget.ListView target = this.Element.
                    //DragListAdapter adapterTarget = (DragListAdapter)_listView.Adapter;
                    BaseAdapter destinationAdapter = (oldParent.Adapter is IWrapperListAdapter)
                            ? ((IWrapperListAdapter)newParent.Adapter).WrappedAdapter as BaseAdapter
                            : ((BaseAdapter)oldParent.Adapter);
                    DragAndDropListViewAdapter destinationDragListAdaptor = destinationAdapter as DragAndDropListViewAdapter;

                    ObservableListCollection <Item> targetList = destinationDragListAdaptor.Collection;

                    int removeLocation = oldParent.GetPositionForView(view);
                    insertLocation = newParent.GetPositionForView(dropItem.View);

                    var item = dropItem.Item as Item;

                    //if (sourceList.Contains(dropItem.Item as Item) && sourceList != targetList)
                    if (sourceList != targetList)
                    {
                        if (insertLocation >= 0)
                        {
                            targetList.Insert(insertLocation, item);
                            targetList.ItemAdded(item);
                        }
                        else
                        {
                            targetList.Add(dropItem.Item as Item);
                            targetList.ItemAdded(item);
                        }

                        sourceList.Remove(item);


                        sourceAdapter.NotifyDataSetChanged();
                        destinationAdapter.NotifyDataSetChanged();
                        v.Visibility = ViewStates.Visible;     //(View.VISIBLE);
                    }
                    else
                    {
                        if (_element.ItemsSource is IOrderable orderable)
                        {
                            if (((IList)_element.ItemsSource).Contains(dropItem.Item))
                            {
                                orderable.ChangeOrdinal(dropItem.OriginalIndex, dropItem.Index);
                            }
                        }
                    }

                    //foreach (var viewTrans in _translatedItems)
                    //{
                    //    viewTrans.TranslationY = 0;
                    //}

                    //_translatedItems.Clear();
                }

                break;

            case DragAction.Ended:
                if (!(v is ListView))
                {
                    return(false);
                }

                //System.Diagnostics.Debug.WriteLine($"DragAction.Ended from {v.GetType()}");

                var mobileItem = (DragItem)e.LocalState;

                mobileItem.View.Visibility = ViewStates.Visible;

                foreach (var view in _translatedItems)
                {
                    view.TranslationY = 0;
                }

                _translatedItems.Clear();

                //if (_element.ItemsSource is IOrderable orderable)
                //{
                //    if (((IList)_element.ItemsSource).Contains(mobileItem.Item))
                //    {
                //        orderable.ChangeOrdinal(mobileItem.OriginalIndex, mobileItem.Index);
                //    }
                //    else
                //    {
                //        orderable.ItemAdded(mobileItem.Item);
                //    }
                //}

                break;
            }

            return(true);
        }