简体   繁体   English

在OnTouch方法中滚动ListView

[英]Scroll ListView in OnTouch method

I am creating an application in xamarin studio and I have a problem with listview. 我在xamarin Studio中创建了一个应用程序,并且listview有问题。 Default ListView working good, List is scrollable and I can go up and go down. 默认的ListView工作正常,列表是可滚动的,我可以上下移动。

I created animation - I move finger to left - day is changing to yesterday and move to right, day is changing to tomorrow. 我创建了动画-我将手指向左移动-日期更改为昨天,而向右移动,则更改为明天。 When I made this animation, my listview isn't scrollable ;s I have this code: 制作此动画时,列表视图不可滚动;我有以下代码:

public bool OnTouch(View v, MotionEvent e)
        {

            switch (e.Action)
            {
            case MotionEventActions.Down:
                _lastX = 0;
                _lastY = 0;
                _viewX = (int)e.GetX ();
                _viewY = (int)e.GetY ();
                break;
            case MotionEventActions.Up:
                if (Math.Abs (_lastX) > 100) {
                    if (_lastX > 0) {
                        date = date.AddDays (-1);
                        DayList.Adapter = new HourInDayAdapter (this, hours, date);
                    } else {
                        date = date.AddDays (1);
                        DayList.Adapter = new HourInDayAdapter (this, hours, date);
                    }
                } else {
                    Toast.MakeText (this, "text", ToastLength.Short).Show ();
                }
                break;
            case MotionEventActions.Move:
                var left = (int)(e.RawX - _viewX);
                var down = (int)(e.RawY - _viewY);
                _lastY = down;
                _lastX = left;
                break;
            }

            return true;
        }

this is C# I think, I should add some method to the MotionEventActions.Move but I don't know which method ;s 我认为这是C#,我应该向MotionEventActions.Move添加一些方法,但是我不知道哪个方法;

No one knows the solution? 没有人知道解决方案吗?

I did that: 我是这样做的:

case MotionEventActions.Move:


                var left = (int)(e.RawX - _viewX);
                var down2 = (int)(_lastY - e.GetY ());
                DayList.SmoothScrollBy (down2, 0);
                _lastY = (int)e.GetY();
                _lastX = left;
                break;
            }

Actually what I would suggest is that you use a GestureDetector inside OnTouch this way you can "tell" when the item was swiped to either left or right. 其实我的建议是,你使用GestureDetectorOnTouch这种方式可以“告诉”当项目被刷到左或右。 GestureDetector is a class that allows you to recognize certain patterns of motions. GestureDetector是一个类,可让您识别某些运动模式。 You can inherit from one of the standard detectors like GestureDetector.SimpleOnGestureListener 您可以继承标准检测器之一,例如GestureDetector.SimpleOnGestureListener

ie

gestureDetector = new GestureDetector(context, YourGestureDetector);

public override bool OnTouch(View v, MotionEvent e)
{
   if (gestureDetector.OnTouchEvent(e) && e.Action != MotionEventActions.Down)
   {
      //you have detected the gesture you were looking for
      //Do something about it
      return true; //because you want to notify that you handled the event so it won't propogare further (optional, depending on what behavior you want)   
   }
   return base.OnTouch(v, e);
}

Here is an example of an implementation of a detector: 这是检测器的实现示例:

class YourGestureDetector : GestureDetector.SimpleOnGestureListener
    {
        //always return true in OnDown
        public override bool OnDown(MotionEvent e)
        {
            return true;
        }

        public override bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
        {
           //detects scrolling motion. return true so it will be picked up by your detector
        }

        public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
           //detects "swiping" motion. return true so it will be picked up by your detector
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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