如何获得GestureDetector正常工作

编程入门 行业动态 更新时间:2024-10-26 22:25:49
本文介绍了如何获得GestureDetector正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个code至今在我的活动:

I have this code so far in my Activity:

private class SwipeGestureDetector extends SimpleOnGestureListener { // Swipe properties, you can change it to make the swipe // longer or shorter and speed private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 200; private static final int SWIPE_THRESHOLD_VELOCITY = 200; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { float diffAbs = Math.abs(e1.getY() - e2.getY()); float diff = e1.getX() - e2.getX(); Log.d("MainDisplayActivity", "Gesture class is running"); if (diffAbs > SWIPE_MAX_OFF_PATH) return false; // Left swipe if (diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { MainDisplayActivity.this.onLeftSwipe(); // Right swipe } else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { MainDisplayActivity.this.onRightSwipe(); } } catch (Exception e) { Log.e("YourActivity", "Error on gestures"); } return false; } }//end of SwipeGestureDetector class //methods called by SwipeGestureDetector when the approrpiate swipes occured private void onLeftSwipe() { Toast.makeText(this, "Successfully have the swipe working for left", Toast.LENGTH_SHORT).show(); } private void onRightSwipe() { Toast.makeText(this, "Successfully have the swipe working for right", Toast.LENGTH_SHORT).show(); }

和我有这样的全球性:私人GestureDetector gestureDetector;

and I have this global: private GestureDetector gestureDetector;

和的onCreate 我有这样的,因为它是我见过的人做的:

and on onCreate I have this because its what I have seen people do:

gestureDetector = new GestureDetector(this, new SwipeGestureDetector()); ((LinearLayout)findViewById(R.id.parent_main_display)).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } });

不是真的知道我做错了,但什么也没发生,当我刷卡。任何想法?

Not really sure what I am doing wrong, but nothing is happening when I swipe. Any ideas?

推荐答案

您应该ovveride下一

You should ovveride the next

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

也onFling应返回true

also onFling should return true

更多推荐

如何获得GestureDetector正常工作

本文发布于:2023-11-24 04:10:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何获得   工作   GestureDetector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!