首页 > 系统 > Android > 正文

Android 滑动拦截实例代码解析

2019-12-12 04:23:37
字体:
来源:转载
供稿:网友

废话不多说了,直接给大家贴代码了,具体代码如下所示:

 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;/** * Created by huqing on 2016/12/7. * 思路: * 分内外部拦截 * 在父布局中,onInterceptTouchEvent先判断是否拦截滑动,true 拦截 直接进入父布局的onTouch方法 ;false 进入子布局的onTouch方法 */public class MyParentView extends LinearLayout{  /**   * 每向下Move移动的距离   */  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;  /**   * 外部拦截,   * 如果是向下滑动 则为true,交给该控件处理,向上false就交给子控件处理吧   * 所以向上的事件 子控件都能获取到的   *   * @param ev   * @return   */  @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;/** * Created by huqing on 2016/12/7. */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 滑动拦截实例代码解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表