对于Android桌面可以用手势左右滑动进行切换。或者大家用过iReader的一定被其丰富的手势折服吧。现在看看自己的多个Activy之间如何切换呢

 

1、需要继承OnGestureListener和OnDoubleTapListener,如下:

Java代码  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">
  1. public class ViewSnsActivity extends Activity implements OnTouchListener, OnGestureListener  

 

2、在添加mGestureDetector的定义,并在ViewSnsActivity的onCreate函数中加入其页面布局的setOnTouchListener事件

Java代码  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">
  1. GestureDetector mGestureDetector;  
    Java代码  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">
  1. public void onCreate(Bundle savedInstanceState) {   
  2.         super.onCreate(savedInstanceState);   
  3.         setContentView(R.layout.view_sns_activity);   
  4.            
  5.         mGestureDetector = new GestureDetector((OnGestureListener) this);     
  6.         LinearLayout viewSnsLayout = (LinearLayout)findViewById(R.id.viewSnsLayout);     
  7.         viewSnsLayout.setOnTouchListener(this);     
  8.         viewSnsLayout.setLongClickable(true);     
  9.     }  
 

3、重载onFling函数

Java代码  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">
  1.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,   
  2.             float velocityY) {   
  3.            
  4.         if (e1.getX()-e2.getX() > snsConstant.getFlingMinDistance()   
  5.                 && Math.abs(velocityX) > snsConstant.getFlingMinVelocity()) {   
  6.   
  7. //          切换Activity   
  8. //          Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);   
  9. //          startActivity(intent);   
  10.             Toast.makeText(this"向左手势", Toast.LENGTH_SHORT).show();   
  11.         } else if (e2.getX()-e1.getX() > snsConstant.getFlingMinDistance()   
  12.                 && Math.abs(velocityX) > snsConstant.getFlingMinVelocity()) {   
  13.                
  14. //          切换Activity   
  15. //          Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);   
  16. //          startActivity(intent);   
  17.             Toast.makeText(this"向右手势", Toast.LENGTH_SHORT).show();   
  18.         }   
  19.            
  20.         return false;     
  21.     }  

其中SnsConstant如下:

Java代码  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">
  1. public class SnsConstant {   
  2.     private static final int FLING_MIN_DISTANCE = 50;     
  3.     private static final int FLING_MIN_VELOCITY = 0;   
  4.        
  5.     public static int getFlingMinDistance() {   
  6.         return FLING_MIN_DISTANCE;   
  7.     }   
  8.     public static int getFlingMinVelocity() {   
  9.         return FLING_MIN_VELOCITY;   
  10.     }   
  11. }  

 

4、重载onTouch函数

Java代码  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">  左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">
  1. public boolean onTouch(View v, MotionEvent event) {   
  2.     return mGestureDetector.onTouchEvent(event);   
  3. }  

 

更多相关文章

  1. android实现点击按钮切换不同的fragment布局
  2. Android中RecyclerView实现横向滑动代码
  3. 【android】多语言与国际化
  4. Android(安卓)SlidingMenu使用详解
  5. schema唤起指定页面
  6. 利用手势放大图片,并限制图片不超过边界,一般用于大图查看
  7. Android屏幕切换
  8. Android实现标题显示隐藏功能
  9. 【原创】Android仿新浪微博的“更多”页面效果

随机推荐

  1. android动画效果演示
  2. Android创建项目
  3. android水平循环滚动控件使用详解
  4. android从网络获取图片
  5. Android NumberPickerDialog
  6. Android 拦截TextView中超链接点击事件
  7. 往Android Studio中导入项目时遇到Instal
  8. android 获取APP大小及其清理缓存内容
  9. 获取当前应用的版本号/应用名称
  10. android用听筒不断播放音乐