学习条记:直接上代码,对了在这里强调一点 onTouch() 与 onTouchEvent() 事故不一样。
先看结构文件:
<?xml version="1.0" encoding="utf-8"?><com.tinno.intercepttouch.MyFrameLayout 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=".MainActivity"> <TextView android:id="@+id/tv" android:layout_width="200dp" android:layout_height="200dp" android:background="#000000" android:text="Hello World!" /></com.tinno.intercepttouch.MyFrameLayout>MyFrameLayout 是一个自界说View:
public class MyFrameLayout extends FrameLayout { public MyFrameLayout(@NonNull Context context) { super(context); } public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println("----onInterceptTouchEvent---ACTION_DOWN"); return true; // break; case MotionEvent.ACTION_POINTER_UP: System.out.println("----onInterceptTouchEvent---ACTION_POINTER_UP"); break; case MotionEvent.ACTION_POINTER_DOWN: System.out.println("----onInterceptTouchEvent---ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_MOVE: System.out.println("----onInterceptTouchEvent---ACTION_MOVE"); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: System.out.println("----onInterceptTouchEvent---ACTION_UP"); break; default: throw new IllegalStateException("Unexpected value: " + event.getActionMasked()); } return false; }}MainActivity:
public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.tv); textView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: System.out.println("----onTouch---ACTION_DOWN"); break; case MotionEvent.ACTION_POINTER_UP: System.out.println("----onTouch---ACTION_POINTER_UP"); break; case MotionEvent.ACTION_POINTER_DOWN: System.out.println("----onTouch---ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_MOVE: System.out.println("----onTouch---ACTION_MOVE"); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: System.out.println("----onTouch---ACTION_UP"); break; default: throw new IllegalStateException("Unexpected value: " + motionEvent.getActionMasked()); } return true; } }); }}好了,开始举行分析:
当 onInterceptTouchEvent 事故返回 true,ViewGroup会将该事故举行拦截,无法向下(View)转达。在 onTouch 中将收不到事故。
当 onTouch 事故返回 true,则表明事故不再向下转达,自己处理惩罚,斲丧掉,例子:该view的 onClick 事故将会失效。
ViewGroup事故转达总结
View事故转达总结
这里必要特殊注意的是,onTouch()的实行 先于onClick()。 |