Android:如何处理从右向左滑动手势
我希望我的应用程序能够识别用户何时在手机屏幕上从右向左滑动。
如何做到这一点?
I want my app to recognize when a user swipes from right to left on the phone screen.
How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
OnSwipeTouchListener.java:
用法:
OnSwipeTouchListener.java:
Usage:
此代码检测左右滑动,避免已弃用的 API 调用,并且比早期答案有其他改进。
像这样使用它:
This code detects left and right swipes, avoids deprecated API calls, and has other miscellaneous improvements over earlier answers.
Use it like this:
如果您还需要处理点击事件,请在此处进行一些修改:
以及示例用法:
If you also need to process click events here some modifications:
And sample usage:
扩展 Mirek 的答案,适用于您想在滚动视图内使用滑动手势的情况。默认情况下,滚动视图的触摸侦听器被禁用,因此不会发生滚动操作。为了解决这个问题,您需要重写
Activity
的dispatchTouchEvent
方法,并在使用完自己的侦听器后返回此方法的继承版本。为了对 Mirek 的代码做一些修改:
我在
OnSwipeTouchListener
中为gestureDetector
添加了一个 getter。将 Activity 内的
OnSwipeTouchListener
声明为类范围字段。相应地修改使用代码:
并覆盖
Activity
中的dispatchTouchEvent
方法:现在滚动和滑动操作都应该可以工作。
Expanding on Mirek's answer, for the case when you want to use the swipe gestures inside a scroll view. By default the touch listener for the scroll view get disabled and therefore scroll action does not happen. In order to fix this you need to override the
dispatchTouchEvent
method of theActivity
and return the inherited version of this method after you're done with your own listener.In order to do a few modifications to Mirek's code:
I add a getter for the
gestureDetector
in theOnSwipeTouchListener
.Declare the
OnSwipeTouchListener
inside the Activity as a class-wide field.Modify the usage code accordingly:
And override the
dispatchTouchEvent
method insideActivity
:Now both scroll and swipe actions should work.
为了拥有
Click Listener
、DoubleClick Listener
、OnLongPress Listener
、向左滑动
、向右滑动< /code>、
向上滑动
、向下滑动
在单个View
上,您需要setOnTouchListener
。即,为此您需要实现
OnTouchListener
的OnSwipeTouchListener
类。In order to have
Click Listener
,DoubleClick Listener
,OnLongPress Listener
,Swipe Left
,Swipe Right
,Swipe Up
,Swipe Down
on SingleView
you need tosetOnTouchListener
. i.e,For this you need
OnSwipeTouchListener
class that implementsOnTouchListener
.@Mirek Rusin 的 Kotlin 版本在这里:
OnSwipeTouchListener.kt :
用法:
open
关键字对我来说是重点......Kotlin version of @Mirek Rusin is here:
OnSwipeTouchListener.kt :
Usage:
the
open
keyword was the point for me...您不需要复杂的计算。
只需使用
GestureDetector
类中的OnGestureListener
接口即可完成。在
onFling
方法中,您可以检测所有四个方向,如下所示:MyGestureListener.java
:用法:
You don't need complicated calculations.
It can be done just by using
OnGestureListener
interface fromGestureDetector
class.Inside
onFling
method you can detect all four directions like this:MyGestureListener.java
:usage:
使用 SwipeListView 并让它为您处理手势检测。
Use SwipeListView and let it handle the gesture detection for you.
要添加 onClick,这就是我所做的。
我正在使用 Fragments,因此使用 getActivity() 作为上下文。这就是我实现它的方式 - 并且它有效。
To add an onClick as well, here's what I did.
I'm using Fragments, so using getActivity() for context. This is how I implemented it - and it works.
由于这个问题有点旧而且很受欢迎,我尝试通过结合答案和评论中的多个建议来更新答案并改进它。
滑动所有四个方向:
用法:
onFling() 的替代方案
替代方案删除了更多代码并重新排序检查。重新排序是没有必要的,但我认为它看起来更整洁,也更容易理解。
代码要放在
try
括号中。用法保持不变,只需实现相应的onSwipe
函数即可
垂直的
一个方向
“向上”的示例
附加说明:
在(ruX 答案)中,他们讨论了删除
onDown
以检测onClickListener 相同的视图。但是,当我删除
onDown
时,该类不再工作。我不知道为什么。我查看了GestureDetector
的代码,但它非常复杂,并且onDown
返回值的结果似乎有一些含义。所以我把它留了下来。此外,您可以像使用onFling
一样使用SimpleOnGestureListener.onSingleTapUp()
并创建一个onClick()
函数那就是那里所说的。版本信息:
Since this Question is somewhat old and very popular I tried to update the answer and improve it by combining multiple suggestions in the answers and comments.
Swipe all four directions:
Usage:
Alternatives for onFling()
The alternatives remove a bit more code and reorder checks. The reorder is not necessary but I think it looks a bit neater and its easier to understand.
The code is to be put in the
try
bracket. The usage stays the same, just implement the respectiveonSwipe
functionsHorizontal
Vertical
One direction
Example for "up"
Additional Notes:
In (ruX answer) they discussed removing
onDown
to also detect anonClickListener
for the same view. However, when I removedonDown
, the class did not work anymore. I don't exactly know why. I looked into the code ofGestureDetector
but it is very complex and the consequences of the return value ofonDown
seems to have some implications. So I left it in. Also, you can just useSimpleOnGestureListener.onSingleTapUp()
just like you didonFling
and create aonClick()
function that is called there.Version Info:
@Edward Brey 的方法效果很好。如果有人也想复制&粘贴
OnSwipeTouchListener
的导入,它们是:@Edward Brey's method works great. If someone would also like to copy & paste the imports for the
OnSwipeTouchListener
, here they are:我的解决方案与上面的类似,但我将手势处理抽象为一个抽象类
OnGestureRegisterListener.java
,其中包括 swipe、click 和 <强>长按手势。OnGestureRegisterListener.java
并像这样使用它。请注意,您还可以轻松传递
View
参数。My solution is similar to those above but I have abstracted the gesture handling into an abstract class
OnGestureRegisterListener.java
, which includes swipe, click and long click gestures.OnGestureRegisterListener.java
And use it like so. Note that you can also easily pass in your
View
parameter.对 @Mirek Rusin 答案进行一些修改,现在您可以检测多点触控滑动。这段代码位于 Kotlin 上:
其中 Gesture.SWIPE_RIGHT 和其他都是手势的唯一整数标识符,我稍后在活动中使用它来检测手势类型:
所以你会看到这里的手势是一个整数变量,它保存我之前传递的值。
A little modification of @Mirek Rusin answer and now you can detect multitouch swipes. This code is on Kotlin:
Where Gesture.SWIPE_RIGHT and others are unique integer indentificator of gesture that I`m using to detect kind of gesture later in my activity:
So you see gesture here is an integer variable that holds value I have passed before.
我一直在做类似的事情,但仅适用于水平滑动
然后它可以用于视图组件
I've been doing similar things, but for horizontal swipes only
And then it can be used for view components
这个问题很多年前就被问过。现在,有一个更好的解决方案:SmartSwipe:https://github.com/luckybilly/SmartSwipe
代码看起来像这样:
This question was asked many years ago. Now, there is a better solution: SmartSwipe: https://github.com/luckybilly/SmartSwipe
code looks like this:
@Mirek Rusin 的回答非常好。
但是,有一个小错误,需要修复 -
有什么区别?仅当我们检查了所有要求(SWIPE_THRESHOLD 和 SWIPE_VELOCITY_THRESHOLD 都为 Ok )时,我们才设置 result = true 。如果某些要求未实现而放弃滑动,这一点很重要,并且我们必须在 OnSwipeTouchListener 的 onTouchEvent 方法中执行一些操作!
@Mirek Rusin answeir is very good.
But, there is small bug, and fix is requried -
What the difference? We set result = true, only if we have checked that all requrinments (both SWIPE_THRESHOLD and SWIPE_VELOCITY_THRESHOLD are Ok ). This is important if we discard swipe if some of requrinments are not achieved, and we have to do smth in onTouchEvent method of OnSwipeTouchListener!
这是用于检测手势方向的简单 Android 代码
在
MainActivity.java
和activity_main.xml
中,编写以下代码:Here is simple Android Code for detecting gesture direction
In
MainActivity.java
andactivity_main.xml
, write the following code:Edward Brey 的回答在 Kotlin 中的用法
the usage of Edward Brey's answer in Kotlin
这个问题仍然存在。添加以下类:
以及
在互联网上找到的类:
然后添加您自己的
OnTouchListener
和OnSwipeTouchListener
,如下所示:This issue still exists. Add the following classes:
and
and a class found on the internet:
then add your own
OnTouchListener
andOnSwipeTouchListener
like so:如果您想在滑动列表项时显示一些带有操作的按钮,互联网上的很多库都有这种行为。
我实现了在互联网上找到的库,我非常满意。它使用起来非常简单而且非常快。我改进了原始库,并为项目单击添加了一个新的单击侦听器。我还添加了很棒的字体库(http://fortawesome.github.io/Font-Awesome/) 现在您可以简单地添加一个新的项目标题并指定 font Awesome 的图标名称。
这里是github链接
If you want to display some buttons with actions when an list item is swipe are a lot of libraries on the internet that have this behavior.
I implemented the library that I found on the internet and I am very satisfied. It is very simple to use and very quick. I improved the original library and I added a new click listener for item click. Also I added font awesome library (http://fortawesome.github.io/Font-Awesome/) and now you can simply add a new item title and specify the icon name from font awesome.
Here is the github link
我知道自 2012 年以来有点晚了,但我希望它会对某人有所帮助,因为我认为它比大多数答案更短、更清晰的代码:
当然,您可以只留下相关的手势。
src: https://developer.android.com/training/gestures/detector
I know its a bit late since 2012 but I hope it will help someone since I think it's a shorter and cleaner code than most of the answers:
of course you can leave only the relevant gestures to you.
src: https://developer.android.com/training/gestures/detector