在 ScrollView 中使用 onTouchListener 检测滑动

2023-11-26

我使用以下代码来检测活动中的滑动:

getWindow().getDecorView().getRootView().setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if(action == MotionEvent.ACTION_DOWN){

                    downX = event.getX();
                    downY = event.getY();
                    //mSwipeDetected = Action.None;
                    return true; // allow other events like Click to be processed
                }
                else if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL){
                     upX = event.getX();
                     upY = event.getY();

                     float deltaX = downX - upX;
                     float deltaY = downY - upY;

                    // horizontal swipe detection
                    if (Math.abs(deltaX) > MIN_DISTANCE) {
                        // left or right
                        if (deltaX < 0) {
                            Log.i(TAG, "Swipe Left to Right");
                            //mSwipeDetected = Action.LR;
                            onClickPrevQues(getWindow().getDecorView().getRootView());

                            return false;
                        }
                        if (deltaX > 0) {
                            Log.i(TAG, "Swipe Right to Left");
                            onClickNextQues(getWindow().getDecorView().getRootView());
                           // mSwipeDetected = Action.RL;
                            return false;
                        }
                    } else if (Math.abs(deltaY) > MIN_DISTANCE) { // vertical swipe
                                                                    // detection
                        // top or down
                        if (deltaY < 0) {
                            Log.i(TAG, "Swipe Top to Bottom");
                            //mSwipeDetected = Action.TB;
                            return false;
                        }
                        if (deltaY > 0) {
                            Log.i(TAG, "Swipe Bottom to Top");
                            //mSwipeDetected = Action.BT;
                            return false;
                        }
                    }
                    return false;
                }
                else if(action == MotionEvent.ACTION_MOVE){
                    return true;
                }
                return false;
            }
        });

该代码在以下情况下工作正常LinearLayout。令人惊讶的是,如果我把它包裹起来,同样的方法不起作用Linearlayout inside ScrollView。这里有什么问题?

Edit:
Layout:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<RelativeLayout 
    android:id="@+id/RelativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:gravity="center_horizontal" >

    <RelativeLayout
        android:id="@+id/RelativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:paddingBottom="0dp"
        android:paddingLeft="620dip"
        android:paddingTop="15dip" >

        <TextView
            android:id="@+id/text_unattempted"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:paddingTop="2dp"
            android:text="TextView"
            android:textColor="#b4cacc"
            android:textStyle="bold" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/RelativeLayout2"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:paddingBottom="5dip"
        android:paddingTop="5dip" >

        <RelativeLayout
            android:id="@+id/relative_group"
            android:layout_width="fill_parent"
            android:layout_height="260dp"
            android:layout_marginLeft="20dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txt_question_nu"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:paddingTop="0dp"
                android:text="Question 1"
                android:textColor="#b4cacc"
                android:textSize="28dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/question"
                android:layout_width="fill_parent"
                android:layout_height="400dp"
                android:layout_alignLeft="@+id/txt_question_nu"
                android:layout_below="@+id/txt_question_nu"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="24dp"
                android:textColor="#000000"
                android:textSize="20dp" />

            <WebView
                android:id="@+id/questionimage"
                android:layout_width="wrap_content"
                android:layout_height="400dp"
                android:layout_alignLeft="@+id/txt_question_nu"
                android:layout_alignParentBottom="true"
                android:layout_below="@+id/txt_question_nu"
                android:layout_marginTop="24dp"
                android:fadeScrollbars="false"
                android:textColor="#000000"
                android:textSize="20dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/hintSolBtn"
                style="@style/HomeButton"
                android:layout_width="40dp"
                android:layout_height="30dp"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/txt_question_nu"
                android:background="#90b7bb"
                android:onClick="onClickHintSol"
                android:scaleType="centerInside"
                android:text="Hint"
                android:textColor="#ffffff" />
        </RelativeLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/radio_question_group"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal"
        android:paddingTop="5dip" >

        <ImageButton
            android:id="@+id/abandonButton"
            style="@style/HomeButton"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="45dp"
            android:onClick="onAbandonAssess"
            android:paddingLeft="6dp"
            android:scaleType="centerInside"
            android:src="@drawable/abandon"
            android:visibility="gone" />

        <Button
            android:id="@+id/submitBtn"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="63dp"
            android:background="#00b6c6"
            android:onClick="onSubmitAssess"
            android:paddingLeft="6dp"
            android:scaleType="centerInside"
            android:text="Submit"
            android:textColor="#ffffff"
            android:textSize="22dp" />

        <ImageButton
            android:id="@+id/prevBtn"
            style="@style/HomeButton"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_weight="0.01"
            android:onClick="onClickPrevQues"
            android:scaleType="centerInside"
            android:src="@drawable/previous" />

        <ImageButton
            android:id="@+id/nextBtn"
            style="@style/HomeButton"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/prevBtn"
            android:onClick="onClickNextQues"
            android:scaleType="centerInside"
            android:src="@drawable/next" />
    </RelativeLayout>

    <TextView
        android:id="@+id/answer_choices"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/LinearLayout1"
        android:layout_marginLeft="37dp"
        android:text="Answer Choices"
        android:textColor="#b4cacc"
        android:textSize="20dp"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/radio_question_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/answer_choices"
        android:layout_below="@+id/answer_choices" >

        <RadioButton
            android:id="@+id/answer1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:button="@drawable/radiostate"
            android:checked="false"
            android:layout_marginTop="10dp"
            android:hapticFeedbackEnabled="true"
            android:height="100dp"
            android:textColor="#000000" />

        <RadioButton
            android:id="@+id/answer2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:button="@drawable/radiostate"
            android:layout_marginTop="10dp"
            android:checked="false"
            android:hapticFeedbackEnabled="true"
            android:textColor="#000000" />

        <RadioButton
            android:id="@+id/answer3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:button="@drawable/radiostate"
            android:layout_marginTop="10dp"
            android:checked="false"
            android:hapticFeedbackEnabled="true"
            android:textColor="#000000" />

        <RadioButton
            android:id="@+id/answer4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:button="@drawable/radiostate"
            android:layout_marginTop="10dp"
            android:checked="false"
            android:hapticFeedbackEnabled="true"
            android:textColor="#000000" />
    </RadioGroup>

    <!-- <ImageView
        android:id="@+id/pen_and_paper_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/pen_paper"
        android:visibility="gone" /> -->

    <ImageButton
        android:id="@+id/erase"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_above="@+id/radio_question_group"
        android:layout_below="@+id/LinearLayout1"
        android:layout_toRightOf="@+id/answer_choices"
        android:onClick="onEraseChoice"
        android:src="@drawable/eraser"
        android:visibility="visible" />

</RelativeLayout>
</ScrollView>

您必须将 OntouchListener 设置为 ScrollView。

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);
scrollView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                downX = event.getX();
                break;
            }
            
            case MotionEvent.ACTION_UP: {
                upX = event.getX();

                float deltaX = downX - upX;

                if (Math.abs(deltaX) > 0) {
                    if (deltaX >= 0) {
                        swipeToRight();
                        return true;
                    } else {
                        swipeToLeft();
                        return true;
                    }
                }
                break;
            }
        }

        return false;
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ScrollView 中使用 onTouchListener 检测滑动 的相关文章

  • Android,让文本切换器成为中心?

    如何集中我的文本切换器 我尝试过设置重力 但似乎不起作用 ts setFactory new ViewFactory public View makeView TextView t new TextView this t setTypefa
  • 相当于Android中的javax.swing.Timer

    有没有类似的东西javax swing Timer在安卓上 我知道如何创建自己的线程 但是有类似摆动计时器的东西吗 您可能正在寻找课程android os CountDownTimer http developer android com
  • 如何在 Android 中创建刮刮卡?

    我需要为我在学校的期末项目创建一个 刮刮卡 应用程序 但找不到如何实现刮刮事件的方法 如何创建背景图像并在其上放置灰色矩形 所以当我刮刮这些矩形时我会看到他们下面的图片 实现必须在 Android 中 因为我还不知道如何在 Objectiv
  • Android,语言文件不起作用

    我现在正在创建一个 Android 应用程序 并尝试为我的母语添加语言文件 但在某种程度上 这对我不起作用 我尝试在两部不同的手机中加载该应用程序 但结果相同 之前创建过语言文件 效果良好 但这次不行 手机设置为瑞典语 语言文件适用于我创建
  • 从Asynctask返回结果

    如果我的 Android 应用程序中有这个后台工作文件 并且它从我的数据库获取数据 我如何将字符串 结果 传递给另一个类 后台工作人员连接到我的服务器 然后使用 php 连接到数据库 public class BackgroundWorke
  • 明文 HTTP 流量...不允许

    我的程序从用户那里获取一个 URL 因此它可以向互联网上的任何网站发出请求 我试图让这成为可能 我查找了有关 Android HTTP Cleartext 错误的所有答案 并做了这个 但它仍然不允许我连接我的测试本地 PHP 服务器 我在这
  • 使用 POST 将数据从 Android 发送到 AppEngine Datastore

    抱歉 如果这是一个简单的问题 但我只是不知道我应该做什么 而且我认为我有点超出了我的深度 我想将数据从 Android 应用程序发送到在 Google App Engine 上运行的应用程序 数据必须从那里写入数据存储区 我的数据主要采用对
  • 用于代码生成的 ANTLR 工具版本 4.7.1 与当前运行时版本 4.5.3 不匹配

    我正在开发一个 Android 应用程序 当前使用 DSL 和一些库 突然构建给了我这个错误 任务 app kaptDebugKotlin 失败 用于代码生成的 ANTLR 工具版本 4 7 1 与当前运行时版本 4 5 3 不匹配 用于解
  • Android 在创建时出现 SQLiteException

    首先我想说我是android新手 所以如果这个问题太愚蠢我很抱歉 我正在为带有两个表的 SQLite 数据库编写一个内容提供程序 表格上是在导航抽屉活动中显示列表 第二个表格是在 ListFragment 中显示 每次启动应用程序时 我都会
  • MAT(Eclipse 内存分析器)- 如何从内存转储中查看位图

    I m analyzing memory usage of my Android app with help of Eclipse Memory Analyzer http www eclipse org mat also known as
  • ImageButton 拉伸背景图像

    我正在尝试创建一个没有边框的 ImageButton 但遇到了图像按钮大小的问题 我使用 Eclipse ADT 将 ImageButton 拖到布局中并选择背景图像 图像按钮显示如下 正如您所看到的 背景图像和图像按钮周边之间有一个边框
  • Android 依赖项:apklib 与 aar 文件

    据我了解 apklib包含代码 共享资源Maven aar文件由以下人员分发Gradle The aar与 apklib 的主要区别在于 类被编译并包含在 aar 根目录下的classes jar 中 然而apklib不能包含已编译的类文件
  • Android - 内容值覆盖现有行

    我正在尝试使用插入值ContentValues 我已将 5 个值插入到 5 列中 运行应用程序后 我只有最后一组值的行ContentValues 前四组未插入 ContentValues cv new ContentValues cv pu
  • 如何在Android中隐藏应用程序标题? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我想隐藏应用程序标题栏 您可以通过编程来完成 import android app Activity import android os
  • EditText 的高度不会扩展到其父级的高度

    我在滚动视图中放置了编辑文本 高度 match parent并期望它的高度等于滚动视图 但事实并非如此 它的高度就像wrap content这意味着如果 EditText 中没有文本 我必须将光标指向要弹出的软键盘的第一 行 我想要的是我可
  • Android apk 调试模式工作正常,但发布模式给出太多警告

    我正在尝试从 eclipse 获取签名的 APK 我有一个可调试的 apk 版本 运行良好 现在发布时 当我尝试使用 Eclipse ADT 进行编译和签名时 我收到很多警告 其中大部分是can t find superclass or i
  • 我可以在主线程上读取一个小 txt 文件,还是应该始终避免那里的 I/O?

    我正在读取一个小的 json 文件 其中有几行 它存储在用户设备的内部应用程序文件夹中 我所做的就是这样 JSONObject jsonObject new JSONObject MyUtils inputStreamToString My
  • 在 Android SDK 中通过单击按钮更改背景颜色不起作用

    我有一个简单的程序 可以在单击按钮后更改背景颜色 但它不起作用 public class ChangeBackgroundActivity extends Activity Called when the activity is first
  • SambaFileInputStream 和 FileInputStream 有什么不同?

    我需要从 samba 服务器流式传输视频 并且我使用 nanohttpd 在我的项目中创建简单的服务器 当我使用本地文件中的 fileinputstream 时 视频视图可以按设置播放视频 http localhost 8080 publi
  • 将 firebase 消息传递添加到 flutter android 项目时出现依赖错误

    我已将 firebase 消息传递添加到我的 Flutter 项目中 在 iOS 上运行良好 在 Android 上运行时出现错误 Android dependency androidx localbroadcastmanager loca

随机推荐