Android-如何检测屏幕上的触摸是“滚动”操作。触摸? [英] Android - how to detect a touch on screen is a "scroll" touch?

查看:309
本文介绍了Android-如何检测屏幕上的触摸是“滚动”操作。触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java创建一个Android应用程序,其中在屏幕上有很多< TextView> ,所有这些都定义了onTouchListeners。它们被包裹在< ScrollView> 中,因为它们占用的空间比屏幕上的可用空间还要多。

I am creating an android app in Java in which I have a lot of <TextView> around the screen, all of them with onTouchListeners defined. They are wrapped in a <ScrollView> because they occupy more space than available in the screen.

我的问题是:当我通过在屏幕上触摸并向上/向下移动手指来上下滚动应用程序时,滚动将按预期方式工作,但是触摸的< TextView> 也被触发(这也是可能的)-我不希望这种情况发生。我希望在触摸屏幕进行滚动时忽略onTouchListener。

My problem is: when I scroll the app, up/down, by touching at the screen and moving my finger up/down, the scroll works as expected but the onTouchListener of the touched <TextView> is also fired (which is probably expected as well) - I don't want that to happen though. I want the onTouchListener to be ignored when I'm touching the screen to scroll it.

如何完成此操作?我不希望用户滚动并在某个< TextView> 上意外触发onTouchListener时运行我的函数。

How can I accomplish this? I don't want my function to run when the user is scrolling and "accidentally" fires the onTouchListener on a certain <TextView>.

推荐答案

更多搜索后,我发现此解决方案。这个想法是检查 ACTION_DOWN ACTION_UP 事件之间的时间是否小于或大于 ViewConfiguration.getTapTimeout()

After searching more, I found this solution by Stimsoni. The idea is to check if the time between the ACTION_DOWN and ACTION_UP events is lower or higher than the value given by ViewConfiguration.getTapTimeout().

从文档中:

[返回]持续时间(以毫秒为单位),我们将等待以查看触摸事件是轻击还是滚动。如果用户未在此间隔内移动,则将其视为轻拍。

[Returns] the duration in milliseconds we will wait to see if a touch event is a tap or a scroll. If the user does not move within this interval, it is considered to be a tap.

代码:

view.setOnTouchListener(new OnTouchListener() {

    private long startClickTime;

    @Override
    public boolean onTouch(View view, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            startClickTime = System.currentTimeMillis();

        } else if (event.getAction() == MotionEvent.ACTION_UP) {

            if (System.currentTimeMillis() - startClickTime < ViewConfiguration.getTapTimeout()) {

                // Touch was a simple tap. Do whatever.

            } else {

                // Touch was a not a simple tap.

            }

        }

        return true;
    }

});

这篇关于Android-如何检测屏幕上的触摸是“滚动”操作。触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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