首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用GestureDetector调用Longpress后调用滚动事件

使用GestureDetector调用Longpress后调用滚动事件
EN

Stack Overflow用户
提问于 2017-04-25 15:09:57
回答 1查看 920关注 0票数 3

在调用Longpress而不释放屏幕(使用手势检测器)之后,我应该如何调用滚动事件?

这是我的班级:

代码语言:javascript
运行
复制
public class TestingGestureDetector extends AppCompatActivity implements GestureDetector.OnGestureListener {
    TextView mTextView;
    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testing_gesture_detector);

        mTextView = (TextView) findViewById(R.id.gesture);
        mTextView.setOnTouchListener(a());
        mGestureDetector = new GestureDetector(this, this);          //


    }


    protected View.OnTouchListener a() {
        return new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mGestureDetector.onTouchEvent(event);           //

                if (event.getAction() == MotionEvent.ACTION_UP)
                    mTextView.setText("Release");
                return true;
            }
        };
    }

    @Override
    public boolean onDown(MotionEvent event) {
        mTextView.setText("Press");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent event) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) {

        mTextView.setText("Move");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent event) {

        mTextView.setText("Long Press");
        //mGestureDetector.setIsLongpressEnabled(false);
    }

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {

        return true;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-04-25 15:25:09

  • 使用setIsLongpressEnabled(isLongpressEnabled)禁用gestureDetector上的长按压。
  • 使用计时器或线程检查0.5s后按下的某些标志的状态。

尝试:

代码语言:javascript
运行
复制
public boolean onTouchEvent(MotionEvent event) {
        if (mGestureDetector.onTouchEvent(event)== true)
        {
            //Fling or other gesture detected (not logpress because it is disabled)
        }
        else
        {
            //Manually handle the event.
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                //Remember the time and press position
                Log.e("test","Action down");
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE)
            {
                //Check if user is actually longpressing, not slow-moving 
                // if current position differs much then press positon then discard whole thing
                // If position change is minimal then after 0.5s that is a longpress. You can now process your other gestures 
                Log.e("test","Action move");
            }
            if (event.getAction() == MotionEvent.ACTION_UP)
            {
                //Get the time and position and check what that was :)
                Log.e("test","Action down");
            }

        }
        return true;
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43614551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档