Android学习笔记之不同activity之间滑动切换

之前学习到ViewFlipper实现一个Activity多个控件之间的滑动切换,现在来学习多个activity之间的滑动切换。

        1.继承OnTouchListner和OnGestureListener.


  1. public class WeatherActivity extends Activity implements OnTouchListener,OnGestureListener{        

  1. //  2.创建GesTureDetector对象(创建时必须实现OnGestureListnener监听器实  

  1. //例) GestureDetector gd;  
  2.  @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.main);  
  6.         gd = new GestureDetector((OnGestureListener)this);      

  1. // 3.为当前Acitivity的布局页面添加setOnTouchListener事件。(OnCreate函数)      

  1. LinearLayout ll = (LinearLayout) findViewById(R.id.weather_layout);  
  2. ll.setOnTouchListener(this);  
  3. ll.setLongClickable(true);  

  1. //注意:若不加setLongClickable(true)的话OnFling会失效,如果不写这句的话OnGestureListener的重写方法OnDown方法返回true也可以。只有这样,view才///能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),我们同样可以通过layout定义中的android:longClickable来做到这一点。   //4.将Acityvity的TouchEvent事件交给GestureDetector处理@Override  
  2.     public boolean onTouch(View v, MotionEvent event) {  
  3.         // TODO Auto-generated method stub  
  4.         return gd.onTouchEvent(event);  
  5.     }      

  1. //  5.重载onFling函数     

  1. @Override  
  2. //e1  The first down motion event that started the fling.手势起点的移动事件    
  3. //e2  The move motion event that triggered the current onFling.当前手势点的移动事件    
  4. //velocityX   The velocity of this fling measured in pixels per second along the x axis.每秒x轴方向移动的像素    
  5. //velocityY   The velocity of this fling measured in pixels per second along the y axis.每秒y轴方向移动的像素    
  6.   
  7.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  8.             float velocityY) {  
  9.         // TODO Auto-generated method stub  
  10.         if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE&&Math.abs(velocityX) > FLING_MIN_VELOCITY)  
  11.         {  
  12.             Intent intent = new Intent(WeatherActivity.this,CityActivity.class);  
  13.             startActivity(intent);  
  14.              Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();   
  15.   
  16.         }  
  17.         else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) >FLING_MIN_VELOCITY) {  
  18.               
  19.             //切换Activity  
  20.             Intent intent = new Intent(WeatherActivity.this, IndexActivity.class);  
  21.             startActivity(intent);  
  22.             Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();  
  23.         }  
  24.           
  25.         return false;  
  26.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值