Android长按滚动

时间:2012-08-23 13:52:55

标签: android scroll long-press

我希望长按“连接”滚动,因此用户无需释放屏幕并开始滚动。

我已经实现了手势检测器......

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之间,用户必须释放屏幕......如何在不释放屏幕的情况下连接此动作?

1 个答案:

答案 0 :(得分:7)

我不认为GestureDetector会做你想做的事情,更有可能你必须自己做。我不知道您当前的设置,下面是一个OnToucListenerScrollView相关联的课程,会考虑这两个事件:

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);
    }

}