2

我正在尝试编写一个Behavior需要拦截触摸事件的自定义。文档说我应该返回 trueonInterceptTouchEvent()以接管事件流,然后只要onTouchEvent()我想继续接收事件就返回 true。当我这样做时,只会收到ACTION_DOWNACTION_CANCEL事件。

以下是我将所有内容连接起来的方式:

//==============================================================================
public class CustomBehavior extends CoordinatorLayout.Behavior<View> {
    //--------------------------------------------------------------------------

    public CustomBehavior( Context context, AttributeSet attrs ) {
        super( context, attrs );
        Log.d( "AppTag", "SlidingCardBehavior" );
    }

    //--------------------------------------------------------------------------

    @Override public boolean onInterceptTouchEvent( CoordinatorLayout coordinator, View child,  MotionEvent event ) {

        Log.d( "AppTag", "onInterceptTouchEvent() "
            + MotionEvent.actionToString( event.getAction() ));

        return true;
    }

    //--------------------------------------------------------------------------

    @Override public boolean onTouchEvent( CoordinatorLayout coordinator, View child, MotionEvent event ) {

        Log.d( "AppTag", "onTouchEvent() "
            + MotionEvent.actionToString( event.getAction() ));

        return true;
    }

    //--------------------------------------------------------------------------
}
//------------------------------------------------------------------------------

布局xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".HomeActivity">

    <FrameLayout android:id="@+id/back"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.v7.widget.CardView
        android:id="@+id/front"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:layout_behavior="tv.shuriken.android.behavior.CustomBehavior" />

</android.support.design.widget.CoordinatorLayout>

当我对此进行测试时,ACTION_DOWN按预期接收,但没有ACTION_MOVEACTION_UP事件是。当我再次触摸时,我立即收到一个ACTION_CANCEL然后另一个ACTION_DOWN

文档使它听起来如此简单,我确信我错过了一些东西。如何获得连续的事件流onTouchEvent()

4

1 回答 1

2

尝试将 clickable="true" 添加到 xml 中的自定义视图中。也不要拦截第一个ACTION_DOWN,拦截ACTION_MOVE。

于 2015-07-23T07:48:36.243 回答