Make a scrollView autoscroll with drag and drop in Android

前端 未结 4 2029
借酒劲吻你
借酒劲吻你 2020-12-17 03:05

I searched all over, but could not find a solution.

I have a view (lets call it myView) inside a scrollview. myView is bigger than the screen. Since I\'m able to get

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 03:51

    I used a timer in In C#

    ScrollCalendar ScrollCalendar = new ScrollCalendar (yourScrollView);
    

    Inside the drag event

        public bool OnDrag (View v, DragEvent e)
        {
            var dragshadow = new EventDateDragShadow (v);
            switch (e.Action) {
            case DragAction.Started:
                return true;
            case DragAction.Entered:
                break;
            case Android.Views.DragAction.Location:
    
                if (e.GetY () < 90) {
                    ScrollCalendar.StartScroll (-15);
                } else if (e.GetY () > yourScrollView.Height - 90) {
                    ScrollCalendar.StartScroll (15);
                } else
                    ScrollCalendar.StopScroll ();
    
                return (true);
            case DragAction.Exited:
                return true;
            case DragAction.Drop:
                return true;
            case DragAction.Ended:
                ScrollCalendar.StopScroll ();
                v.SetOnDragListener (null);
                return true;
            }
    
            return true;
        }
    

    The ScrollCalendar class

    public class ScrollCalendar
    {
        private ScrollView Calendar;
        private System.Timers.Timer Timer;
        private int ScrollDistance;
    
        public ScrollCalendar(ScrollView calendar)
        {
            Calendar = calendar;
            Timer = new System.Timers.Timer();
            Timer.Elapsed+=new ElapsedEventHandler(Scroll);
            Timer.Interval = 50;
        }
    
        public void StartScroll(int scrollDistance)
        {
            if (Timer.Enabled) {
                return;
            }
            ScrollDistance = scrollDistance;
            Timer.Enabled = true;
        }
    
        public void StopScroll()
        {
            Timer.Enabled = false;
        }
    
        private void Scroll(object source, ElapsedEventArgs e)
        {
            Calendar.SmoothScrollBy (0, ScrollDistance);
        }
    
    }
    

    Change the StartScroll value and the Timer.Interval to adjust the speed of the scroll.

提交回复
热议问题