Skip to content Skip to sidebar Skip to footer

Android Long Press With Scroll

I want to 'connect' long press with scroll, so the user doesn't have to release the screen and start to scroll. I have gesture detector implemented... final GestureDetector gesture

Solution 1:

I don't think a GestureDetector will do what you want, more likely you'll have to do it yourself. I don't know your current setup, below is a class with a OnToucListener tied to a ScrollView which will take in consideration both events:

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

}

Post a Comment for "Android Long Press With Scroll"