2012-08-23 34 views
1

我想長時間按住滾動鍵「連接」,因此用戶不必釋放屏幕並開始滾動。帶滾動功能的Android長按

我已經姿態探測器實現...

final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() { 
    public void onLongPress(MotionEvent e) { 
     // action 1 
    } 

    public boolean onScroll(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { 
     // action 2 
    }  
} 

public boolean onTouchEvent(MotionEvent event) { 
    return gestureDetector.onTouchEvent(event); 
}  

但現在動作1和動作2,用戶之間不得不釋放屏幕...我如何連接這個動作不釋放屏幕?

回答

6

我不認爲GestureDetector會做你想做的事,更有可能你必須自己做。我不知道你當前的設置,下面是一個OnToucListener綁在ScrollView一類將在考慮這兩個事件:

public class ScrollTouchTest extends Activity { 

    private final int LONG_PRESS_TIMEOUT = ViewConfiguration 
      .getLongPressTimeout(); 
    private Handler mHandler = new Handler(); 
    private boolean mIsLongPress = false; 
    private Runnable longPress = new Runnable() { 

     @Override 
     public void run() {   
      if (mIsLongPress) {    
       actionOne(); 
       mIsLongPress = false; 
      } 
     } 

    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.views_scrolltouchtest); 
     findViewById(R.id.scrollView1).setOnTouchListener(
       new OnTouchListener() { 

        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         final int action = event.getAction(); 
         switch (action) { 
         case MotionEvent.ACTION_DOWN: 
          mIsLongPress = true;        
          mHandler.postDelayed(longPress, LONG_PRESS_TIMEOUT); 
          break; 
         case MotionEvent.ACTION_MOVE: 
          actionTwo(event.getX(), event.getY()); 
          break; 
         case MotionEvent.ACTION_CANCEL: 
         case MotionEvent.ACTION_UP: 
          mIsLongPress = false; 
          mHandler.removeCallbacks(longPress); 
          break; 
         } 
         return false; 
        } 
       }); 
    } 

    private void actionOne() { 
     Log.e("XXX", "Long press!!!"); 
    } 

    private void actionTwo(float f, float g) { 
     Log.e("XXX", "Scrolling for X : " + f + " Y : " + g); 
    } 

} 
+0

您onTouch(視圖V,MotionEvent事件)如果你想應該返回true處理ACTION_MOVE ...事件 – ebtokyo