2

長押しとスクロールを「接続」したいので、ユーザーは画面を離してスクロールを開始する必要はありません。

ジェスチャー検出器を実装しました...

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の間で、ユーザーは画面を解放する必要があります...画面を解放せずにこのアクションを接続するにはどうすればよいですか??

4

1 に答える 1

8

私はGestureDetectorあなたが望むことをするつもりはないと思います。あなたの現在の設定はわかりません。以下は、両方のイベントを考慮にOnToucListener入れる a に関連付けられたクラスです。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);
    }

}
于 2012-08-28T09:20:21.750 に答える