我正在尝试编写一个Behavior
需要拦截触摸事件的自定义。文档说我应该返回 trueonInterceptTouchEvent()
以接管事件流,然后只要onTouchEvent()
我想继续接收事件就返回 true。当我这样做时,只会收到ACTION_DOWN
和ACTION_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_MOVE
或ACTION_UP
事件是。当我再次触摸时,我立即收到一个ACTION_CANCEL
然后另一个ACTION_DOWN
。
文档使它听起来如此简单,我确信我错过了一些东西。如何获得连续的事件流onTouchEvent()
?