Android中手势滑动翻页之GestureDetector总结

写作缘由:最近在做左右滑动翻页效果,代码写出来经过3天调试毫无进展,经过网上多次搜索和自己亲自实现,在这里写个总结供自己学习也为大家提供个方便,避免以后大家走弯路,由于我写的项目比较繁琐,就不自己写Demo,直接转载一篇,只是提醒其中自己遇到的坑

      提示01: 网上都说要触发onScroll和onFling,必须让监听器的onDown的返回值设为true,本人测试false也可,目前没问题!所以没有实现翻页不要纠结再次处!

   提示02: 我加载的是webview页面,所以这里必须要注意:一般我们用于接收GestureDetector对象的方法是OnTouchevent();,而在View组件占用了屏幕空间之后,这个方法就无效了,只有换成 dispatchTouchEvent方法才有效!这个方法贴个代码解释:

@Override  

public boolean dispatchTouchEvent(MotionEvent ev) { //注意这里不能用ONTOUCHEVENT方法,不然无效的  

Toast.makeText(NewsContent.this, "jinru", 1).show();  

detector.onTouchEvent(ev);  

webview.onTouchEvent(ev);//这几行代码也要执行,将webview载入MotionEvent对象一下,这块加载webview必须加 

return super.dispatchTouchEvent(ev);  

}

提示03: 下来就是贴代码了,代码和网上几乎相同这个(提示02的方法必须加到代码中!!!!)我是菜鸟不喜勿喷,也欢迎大神指点!

转载代码来自:转载于此,点击链接

  

  1. import com.stone.R;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.GestureDetector;  
  6. import android.view.GestureDetector.OnGestureListener;  
  7. import android.view.MotionEvent;  
  8. import android.widget.ViewFlipper;  
  9.   
  10. //ViewFlipper 和手势 左右滑动 切换 Activity  
  11. public class Gesture extends Activity implements OnGestureListener{  
  12.     ViewFlipper flipper; //一次显示一个子view  
  13.     GestureDetector detector; //手势监测器  
  14.       
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.           
  19.         detector = new GestureDetector(this);  
  20.         setContentView(R.layout.flipper);  
  21.         flipper = (ViewFlipper) findViewById(R.id.vf_flipper);  
  22.     }  
  23.       
  24.     @Override  
  25.     public boolean onTouchEvent(MotionEvent event) {  
  26.         return this.detector.onTouchEvent(event);//由检测器 执行 activity.<span style="font-family: Arial, Helvetica, sans-serif;">onTouchEvent</span>  
  27.   
  28.     }  
  29.       
  30.     /*以下为 OnGestureListener 的方法*/  
  31.     @Override   
  32.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  33.             float velocityY) {  
  34.         if (e1.getX() - e2.getX() > 120) {//向左滑,右边显示  
  35.             //this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));  
  36.             //this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));   
  37.             this.flipper.showNext();   
  38.         }  
  39.         if (e1.getX() - e2.getX() < -120) {//向右滑,左边显示  
  40.             this.flipper.showPrevious();   
  41.         }  
  42.         return false;  
  43.     }  
  44.       
  45.     @Override  
  46.     public boolean onDown(MotionEvent e) {  
  47.         return false;  
  48.     }  
  49.     @Override  
  50.     public void onShowPress(MotionEvent e) {  
  51.           
  52.     }  
  53.     @Override //单击  
  54.     public boolean onSingleTapUp(MotionEvent e) {  
  55.         return false;  
  56.     }  
  57.     @Override  
  58.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
  59.         return false;  
  60.     }  
  61.     @Override // 长按  
  62.     public void onLongPress(MotionEvent e) {  
  63.           
  64.     }  
  65.       
  66. }  


flipper.xml

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ViewFlipper  
  8.         android:id="@+id/vf_flipper"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" >  
  11.   
  12.         <ImageView  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="fill_parent"  
  15.             android:src="@drawable/a2" />  
  16.   
  17.         <ImageView  
  18.             android:layout_width="fill_parent"  
  19.             android:layout_height="fill_parent"  
  20.             android:src="@drawable/a4" />  
  21.   
  22.         <ImageView  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="fill_parent"  
  25.             android:src="@drawable/a3" />  
  26.     </ViewFlipper>  
  27.   
  28. </LinearLayout>  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值