什么导致Android中的MotionEvent.ACTION_CANCEL?

时间:2012-08-14 21:16:02

标签: android debugging motionevent

我正在调试Android上的一些触摸处理内容,并且我正在尝试找出发送到我的View onTouchListener的MotionEvent包含取消操作的原因。我无法找到有关其原因的任何文档,并希望有人能指出我正确的方向来调试此问题 - 错误代码,源代码或一些常识。

4 个答案:

答案 0 :(得分:52)

这就是你要找的东西:

“当父母拥有动议时,会发生ACTION_CANCEL, 例如,当用户在列表视图中拖动足够的时候 开始滚动而不是让你按下里面的按钮。 您可以在viewgroup文档中找到有关它的更多信息:onInterceptTouchEvent。“

希望这是您正在寻找的答案:

资源:Motion EventStack Overflow

答案 1 :(得分:0)

您只需要打电话

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/category_list_cardview"
    android:layout_margin="2.0dip"
    android:foreground="?android:selectableItemBackground"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="2.0dip"
    app:cardElevation="2.0dip"
    app:cardUseCompatPadding="true">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="right"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:minHeight="?actionBarSize">


            <ImageView
                android:id="@+id/ImageView_Category"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:src="@drawable/ic_home_black_24dp"
                android:layout_weight="0.005"/>

            <TextView
                android:id="@+id/TextView_Category"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Category"
                android:textStyle="bold"
                android:textColor="#000"
                android:textSize="25dp"
                android:gravity="center|right"
                android:layout_weight="0.01"/>

        </LinearLayout>

在父视图上,像这样-

requestDisallowInterceptTouchEvent(true);

来源:Screenshot

答案 2 :(得分:0)

ACTION_CANCEL由祖先触发,以通知所有后代他们失去了onTouch的控制权,它将负责处理下一个onTouch事件。通常是由后代在onTouchonTouchEvent方法中返回true引起的,但是此后,在下一次手势触摸事件期间,祖先在onInterceptTouchEvent()

中返回true。

请看看diagram

答案 3 :(得分:-1)

当拖动移出视图rect时,您将获得ACTION_CANCEL