2014-10-29 58 views
0

如何检测在ListView上上下滑动。我曾尝试以下方法在ListView上检测滑动

  1. 使用onSimpleGestureListener,onFling()适用于一扔(即当我刷卡后离开屏幕)。但它不会被称为滑动(手指最终没有从屏幕上抬起)。

2.在onSimpleGestureListener的onScroll()中,distanceY对于检测向上和向下滑动没有帮助。它可以正常工作,但可以在特定的滑动条件下将其值从负值变为正值。

  • 使用onTouchListener

    public boolean onTouch(View v, MotionEvent event) { 
    
         return gestureDetector.onTouchEvent(event); 
         switch (event.getAction()) { 
    
          case MotionEvent.ACTION_DOWN: 
           before = event.getY(); 
           break; 
          case MotionEvent.ACTION_MOVE: 
           now = event.getY(); 
    
           if (now < before) { 
    
             upSwipe() 
    
           } else if (now > before) { 
    
             downSwipe(); 
    
           } 
    
         } 
         return false; 
        } 
    

    当我向上滑动,可变now有时变得比可变previous,有时小大。所以调用upSwipe()和downSwipe()。

  • 我几个小时都在b my我的脑袋。无法解决这个问题。

    回答

    -1

    您是否试图返回true而不是返回false。基于Detecting Common Gestures documentation

    “要小心创建一个侦听器,该侦听器为ACTION_DOWN事件返回false如果这样做,将不会为随后的ACTION_MOVE和ACTION_UP事件调用侦听器,这是因为ACTION_DOWN是所有触摸的起点事件“。