欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 滑动拦截实例代码解析

程序员文章站 2024-02-28 14:52:34
废话不多说了,直接给大家贴代码了,具体代码如下所示: package demo.hq.com.fby; import android.content.cont...

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

 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 滑动拦截实例代码解析,希望对大家有所帮助