本文介绍了在滚动型使用onTouchListener检测刷卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code检测我的活动刷卡:

  getWindow()。getDecorView()。getRootView()。setOnTouchListener(新OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                INT行动= event.getAction();
                如果(动作== MotionEvent.ACTION_DOWN){                    downX = event.getX();
                    霜霉病= event.getY();
                    // mSwipeDetected = Action.None;
                    返回true; //允许其他活动,如点击被处理
                }
                否则,如果(动作== || MotionEvent.ACTION_UP行动== MotionEvent.ACTION_CANCEL){
                     UPX = event.getX();
                     upY = event.getY();                     浮DELTAX = downX - UPX;
                     浮DELTAY =柔和 - upY;                    //水平轻扫检测
                    如果(Math.abs(DELTAX)GT; MIN_DISTANCE){
                        // 左还是右
                        如果(DELTAX℃,){
                            Log.i(TAG,向左或向右滑动);
                            // mSwipeDetected = Action.LR;
                            的onClick prevQues(getWindow()getDecorView()getRootView());                            返回false;
                        }
                        如果(DELTAX大于0){
                            Log.i(TAG,刷卡从右至左);
                            onClickNextQues(getWindow()getDecorView()getRootView());
                           // mSwipeDetected = Action.RL;
                            返回false;
                        }
                    }否则如果(Math.abs(DELTAY)GT; MIN_DISTANCE){//垂直刷卡
                                                                    //检测
                        //顶或向下
                        如果(DELTAY℃,){
                            Log.i(TAG,刷卡从上到下);
                            // mSwipeDetected = Action.TB;
                            返回false;
                        }
                        如果(DELTAY大于0){
                            Log.i(TAG,刷卡从下到上);
                            // mSwipeDetected = Action.BT;
                            返回false;
                        }
                    }
                    返回false;
                }
                否则,如果(动作== MotionEvent.ACTION_MOVE){
                    返回true;
                }
                返回false;
            }
        });

在code在的LinearLayout 的情况下正常工作。令人惊讶的是同样是在不工作的情况下,如果我换行的LinearLayout 滚动型。有什么问题吗?

编辑:结果
布局:

 <?XML版本=1.0编码=UTF-8&GT?;
<滚动型
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT>
< RelativeLayout的
    机器人:ID =@ + ID / RelativeLayout1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=#FFFFFF
    机器人:重力=CENTER_HORIZONTAL>    < RelativeLayout的
        机器人:ID =@ + ID / RelativeLayout2
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=CENTER_HORIZONTAL
        机器人:方向=横向
        机器人:paddingBottom会=0dp
        机器人:paddingLeft =620dip
        机器人:paddingTop =15dip>        <的TextView
            机器人:ID =@ + ID / text_unattempted
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentTop =真
            机器人:paddingTop =2DP
            机器人:文字=TextView的
            机器人:文字颜色=#b4cacc
            机器人:文字样式=大胆/>
    < / RelativeLayout的>    <的LinearLayout
        机器人:ID =@ + ID / LinearLayout1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / RelativeLayout2
        机器人:重力=CENTER_HORIZONTAL
        机器人:方向=横向
        机器人:paddingBottom会=5dip
        机器人:paddingTop =5dip>        < RelativeLayout的
            机器人:ID =@ + ID / relative_group
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =260dp
            机器人:layout_marginLeft =20dp
            机器人:方向=垂直>            <的TextView
                机器人:ID =@ + ID / txt_question_nu
                机器人:layout_width =180dp
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignParentLeft =真
                机器人:layout_alignParentTop =真
                机器人:paddingTop =0dp
                机器人:文字=问题1
                机器人:文字颜色=#b4cacc
                机器人:TEXTSIZE =28dp
                机器人:文字样式=大胆/>            <的TextView
                机器人:ID =@ + ID /问题
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =400dp
                机器人:layout_alignLeft =@ + ID / txt_question_nu
                机器人:layout_below =@ + ID / txt_question_nu
                机器人:layout_marginBottom =20dp
                机器人:layout_marginTop =24dp
                机器人:文字颜色=#000000
                机器人:TEXTSIZE =20dp/>            <的WebView
                机器人:ID =@ + ID / questionimage
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =400dp
                机器人:layout_alignLeft =@ + ID / txt_question_nu
                机器人:layout_alignParentBottom =真
                机器人:layout_below =@ + ID / txt_question_nu
                机器人:layout_marginTop =24dp
                机器人:fadeScrollbars =假
                机器人:文字颜色=#000000
                机器人:TEXTSIZE =20dp
                机器人:文字样式=大胆/>            <按钮
                机器人:ID =@ + ID / hintSolBtn
                风格=@风格/为homeButton
                机器人:layout_width =40dp
                机器人:layout_height =30dp
                机器人:layout_alignParentTop =真
                机器人:layout_toRightOf =@ + ID / txt_question_nu
                机器人:背景=#90b7bb
                安卓的onClick =onClickHintSol
                机器人:scaleType =centerInside
                机器人:文字=暗示
                机器人:文字颜色=#FFFFFF/>
        < / RelativeLayout的>
    < / LinearLayout中>    < RelativeLayout的
        机器人:ID =@ + ID / LinearLayout2
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_marginTop =15dp
        机器人:layout_below =@ + ID / radio_question_group
        机器人:layout_alignParentLeft =真
        机器人:方向=横向
        机器人:paddingTop =5dip>        <的ImageButton
            机器人:ID =@ + ID / abandonButton
            风格=@风格/为homeButton
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =50dp
            机器人:layout_alignParentRight =真
            机器人:layout_marginRight =45dp
            安卓的onClick =onAbandonAssess
            机器人:paddingLeft =6DP
            机器人:scaleType =centerInside
            机器人:SRC =@绘制/摒弃
            机器人:知名度=水涨船高/>        <按钮
            机器人:ID =@ + ID / submitBtn
            机器人:layout_width =150dp
            机器人:layout_height =50dp
            机器人:layout_alignParentRight =真
            机器人:layout_centerVertical =真
            机器人:layout_marginRight =63dp
            机器人:背景=#00b6c6
            安卓的onClick =onSubmitAssess
            机器人:paddingLeft =6DP
            机器人:scaleType =centerInside
            机器人:文字=提交
            机器人:文字颜色=#FFFFFF
            机器人:TEXTSIZE =22dp/>        <的ImageButton
            机器人:ID =@ + ID / prevBtn
            风格=@风格/为homeButton
            机器人:layout_width =120dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_weight =0.01
            安卓的onClick =的onClick prevQues
            机器人:scaleType =centerInside
            机器人:SRC =@绘制/ previous/>        <的ImageButton
            机器人:ID =@ + ID / nextBtn
            风格=@风格/为homeButton
            机器人:layout_width =120dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentTop =真
            机器人:layout_toRightOf =@ + ID / prevBtn
            安卓的onClick =onClickNextQues
            机器人:scaleType =centerInside
            机器人:SRC =@绘制/下一个/>
    < / RelativeLayout的>    <的TextView
        机器人:ID =@ + ID / answer_choices
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / LinearLayout1
        机器人:layout_marginLeft =37dp
        机器人:文字=答案选项
        机器人:文字颜色=#b4cacc
        机器人:TEXTSIZE =20dp
        机器人:文字样式=大胆/>    < RadioGroup中
        机器人:ID =@ + ID / radio_question_group
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / answer_choices
        机器人:layout_below =@ + ID / answer_choices>        <单选
            机器人:ID =@ + ID / ANSWER1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:按钮=@绘制/ radiostate
            安卓:检查=假
            机器人:layout_marginTop =10dp
            机器人:hapticFeedbackEnabled =真
            机器人:身高=100dp
            机器人:文字颜色=#000000/>        <单选
            机器人:ID =@ + ID / ANSWER2
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:按钮=@绘制/ radiostate
            机器人:layout_marginTop =10dp
            安卓:检查=假
            机器人:hapticFeedbackEnabled =真
            机器人:文字颜色=#000000/>        <单选
            机器人:ID =@ + ID / ANSWER3
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:按钮=@绘制/ radiostate
            机器人:layout_marginTop =10dp
            安卓:检查=假
            机器人:hapticFeedbackEnabled =真
            机器人:文字颜色=#000000/>        <单选
            机器人:ID =@ + ID / answer4
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:按钮=@绘制/ radiostate
            机器人:layout_marginTop =10dp
            安卓:检查=假
            机器人:hapticFeedbackEnabled =真
            机器人:文字颜色=#000000/>
    < / RadioGroup中>    <! - < ImageView的
        机器人:ID =@ + ID / pen_and_paper_image
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_centerVertical =真
        机器人:SRC =@绘制/ pen_paper
        机器人:知名度=水涨船高/> - >    <的ImageButton
        机器人:ID =@ + ID /擦除
        机器人:layout_width =35dp
        机器人:layout_height =35dp
        机器人:layout_above =@ + ID / radio_question_group
        机器人:layout_below =@ + ID / LinearLayout1
        机器人:layout_toRightOf =@ + ID / answer_choices
        安卓的onClick =onEraseChoice
        机器人:SRC =@绘制/橡皮擦
        机器人:知名度=看得见/>< / RelativeLayout的>
< /滚动型>


解决方案

我觉得这是你在找什么:

在android的滚动型刷卡事件

I'm using the following code to detect swipe in my Activity:

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;
            }
        });

The code is working fine in case of LinearLayout. Surprisingly the same is not working in case if I wrap the Linearlayout inside ScrollView. What is the problem here?

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>
解决方案

I think this is what you are looking for:

Swipe event in android ScrollView

这篇关于在滚动型使用onTouchListener检测刷卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:33