Skip to content

Commit

Permalink
事件拦截机制验证
Browse files Browse the repository at this point in the history
  • Loading branch information
daleige committed Sep 3, 2019
1 parent c5e7544 commit 3327035
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
package com.cyq.toucheventdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private static final String Log = "TouchEventDemo";
private MyView myView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
android.util.Log.d(Log, "my_view:手指按下");
break;
case MotionEvent.ACTION_MOVE:
android.util.Log.d(Log, "my_view:手指移动");
break;
case MotionEvent.ACTION_UP:
android.util.Log.d(Log, "my_view:手指抬起");
break;
}
return false;
}
});
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
android.util.Log.d(Log, "MainActivity:dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
android.util.Log.d(Log, "MainActivity:onTouchEvent");
return super.onTouchEvent(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cyq.toucheventdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

/**
* Create by 陈扬齐
* Create on 2019-09-03
* description:
*/
public class MyButton extends Button {
public MyButton(Context context) {
super(context);
}

public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.cyq.toucheventdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
* Create by 陈扬齐
* Create on 2019-09-03
* description:
*/
public class MyScrollView extends ScrollView {
public MyScrollView(Context context) {
super(context);
}

public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean result = super.onInterceptTouchEvent(ev);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.cyq.toucheventdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
* Create by 陈扬齐
* Create on 2019-09-03
* description:
*/
public class MyView extends View {
private static final String Log = "TouchEventDemo";

public MyView(Context context) {
super(context);
}

public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
android.util.Log.d(Log, "MyView:dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
android.util.Log.d(Log, "MyView:onTouchEvent");
return super.onTouchEvent(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.cyq.toucheventdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* Create by 陈扬齐
* Create on 2019-09-03
* description:
*/
public class MyViewGroup extends FrameLayout {
private static final String Log = "TouchEventDemo";

public MyViewGroup(@NonNull Context context) {
super(context);
}

public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
android.util.Log.d(Log, "MyViewGroup:dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
android.util.Log.d(Log, "MyViewGroup:onInterceptTouchEvent");
return super.onInterceptTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
android.util.Log.d(Log, "MyViewGroup:onTouchEvent");
// switch (event.getAction()) {
// case MotionEvent.ACTION_DOWN:
// android.util.Log.d(Log, "MyViewGroup:手指按下");
// break;
// case MotionEvent.ACTION_MOVE:
// android.util.Log.d(Log, "MyViewGroup:手指移动");
// break;
// case MotionEvent.ACTION_UP:
// android.util.Log.d(Log, "MyViewGroup:手指抬起");
// break;
// }

return super.onTouchEvent(event);
}
}
19 changes: 8 additions & 11 deletions TouchEventDemo/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.cyq.toucheventdemo.MyViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.cyq.toucheventdemo.MyView
android:id="@+id/my_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@color/colorAccent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</com.cyq.toucheventdemo.MyViewGroup>

0 comments on commit 3327035

Please sign in to comment.