废话不多说了,直接给大家贴代码了,具体代码如下所示:
package demo.hq.com.fby;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
public class MyParentView extends LinearLayout
{
private int mMove;
private int yDown;
private int yMove;
private int downDistance = 0;
public MyParentView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
boolean intercept = false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
switch (ev.getAction())
{
case MotionEvent.ACTION_DOWN:
yDown =(int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
yMove = (int) ev.getY();
if (yMove > yDown)
{
intercept = true;
Log.d("hqq", "拦截~~~~~~~~~~~~~~~~~~");
}
else if (yMove < yDown)
{
intercept = false;
Log.d("hqq", "不拦截~~~~~~~~~~~~~~~~~~");
}
break;
case MotionEvent.ACTION_UP:
break;
}
//true 拦截,进入该控件的onTouchEvent方法 false:进入子控件的OnTouchEvent
boolean returnInterCept = intercept;
intercept = false;
return returnInterCept;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
Log.e("hq", "father onTouch");
int y = (int) event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
yDown = y;
break;
case MotionEvent.ACTION_MOVE:
if (downDistance>=250){
}else {
yMove = y;
if (yMove - yDown > 0)
{
mMove = yMove - yDown;
downDistance += mMove;
if (downDistance>=250){
layout(getLeft(),downDistance, getRight(), getHeight() + downDistance);
}else {
layout(getLeft(), getTop() + mMove, getRight(), getBottom() + mMove);
}
}
}
break;
case MotionEvent.ACTION_UP:
layout(getLeft(), getTop() - downDistance, getRight(), getBottom() - downDistance);
downDistance = 0;
break;
}
return true;// return super.onTouchEvent(event);
}
}
package demo.hq.com.fby;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class MyScrollView extends ScrollView
{
public MyScrollView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
Log.e("hq","child onTouch----------------");
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
if (getScrollY()==0){//ScrollView没有滑动时 ,即滑动高度没变化的话就允许父控件拦截
getParent().requestDisallowInterceptTouchEvent(false);
}else {//禁止拦截
getParent().requestDisallowInterceptTouchEvent(true);
}
break;
}
return super.onTouchEvent(ev);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
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="@drawable/bg"
tools:context="demo.hq.com.fby.MainActivity">
<demo.hq.com.fby.MyParentView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text=" World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text=" World!"/>
<demo.hq.com.fby.MyScrollView android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:text="Hello World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:text="Hello World!"/>
</LinearLayout>
</demo.hq.com.fby.MyScrollView>
</demo.hq.com.fby.MyParentView>
</RelativeLayout>
以上所述是小编给大家介绍的Android 滑动拦截实例代码解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!
您可能感兴趣的文章:Android实现图片左右滑动效果Android实现图片自动轮播并且支持手势左右无限滑动Android使用ViewPager实现无限滑动效果Android的OkHttp包中的HTTP拦截器Interceptor用法示例Android编程实现拦截短信并屏蔽系统Notification的方法Android开发四大组件之实现电话拦截和电话录音Android拦截外拨电话程序示例android短信拦截的实现代码