Android 滑動攔截執行個體代碼解析_Android

來源:互聯網
上載者:User

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:

 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 滑動攔截執行個體代碼解析,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.