Android长按滚动 [英] Android long press with scroll

查看:65
本文介绍了Android长按滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过滚动长按来连接",这样用户就不必释放屏幕并开始滚动.

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

But now between action 1 and action 2, user have to release the screen... How can I connect this actions without releasing the screen??

推荐答案

我不认为GestureDetector会做您想要的事情,更有可能您必须自己做.我不知道您当前的设置,下面是一个将OnToucListener绑定到ScrollView的类,它将同时考虑两个事件:

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

}

这篇关于Android长按滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆