带有ListView的界面左右滑动,切换界面。 (手势监听)

 带有ListView界面左右滑动,切换界面

 

相信大家在做OnGestureListener滑动切换窗口的时候,会遇到这样的问题。就是当界面中含有listview的时候,OnGestureListener的左右触屏滑动就被listview自己吃掉了。

翻看api帮助文档和自己的一些理解,决定从对listview重写开始,开解决这个头疼的问题。

 

测试的源码:

http://download.csdn.net/detail/wanli_smile/4182584(推荐这个)

http://download.csdn.net/detail/wanli_smile/4178852

 

开始说明了:新建一个baseatvity实现Gesture滑动切换界面,让其他的activity继承它,这样其他的activity也可以继承它的滑动切换功能,提高代码复用。

下面是BaseActivity:

  1. package com.gesturetest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.util.Log;  
  6. import android.view.GestureDetector;  
  7. import android.view.GestureDetector.OnGestureListener;  
  8. import android.view.MotionEvent;  
  9. /** 
  10.  *  
  11.  * @author liwan 
  12.  *所有activity的基类。继承这个activity的都具有滑动功能哦 
  13.  */  
  14. public class BaseActivity extends Activity implements OnGestureListener{  
  15.     /** 
  16.      * 手势监听 
  17.      */  
  18.     GestureDetector gestureDetector=new GestureDetector(this);  
  19.     /** 
  20.      * 标志位 
  21.      */  
  22.     public static int flag=0;  
  23.     @SuppressWarnings("rawtypes")  
  24.       
  25.     /** 
  26.      * 所有切换的activity 
  27.      */  
  28.     static Class[] myClass=new Class[]{TestActivity.class,GestureTestActivity.class};  
  29.   
  30.     @Override  
  31.     public boolean onTouchEvent(MotionEvent event) {  
  32.         // TODO Auto-generated method stub  
  33.         return gestureDetector.onTouchEvent(event);  
  34.     }  
  35.     @Override  
  36.     public boolean onDown(MotionEvent e) {  
  37.         // TODO Auto-generated method stub  
  38.         return false;  
  39.     }  
  40.   
  41.     @Override  
  42.     public void onShowPress(MotionEvent e) {  
  43.         // TODO Auto-generated method stub  
  44.           
  45.     }  
  46.   
  47.     @Override  
  48.     public boolean onSingleTapUp(MotionEvent e) {  
  49.         // TODO Auto-generated method stub  
  50.         return false;  
  51.     }  
  52.   
  53.     @Override  
  54.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  55.             float distanceY) {  
  56.         // TODO Auto-generated method stub  
  57.         return false;  
  58.     }  
  59.   
  60.     @Override  
  61.     public void onLongPress(MotionEvent e) {  
  62.         // TODO Auto-generated method stub  
  63.           
  64.     }  
  65.   
  66.     @Override  
  67.     /** 
  68.      * 滑动事件的处理 
  69.      */  
  70.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  71.             float velocityY) {  
  72.           Log.i("Fling""baseactivity");    
  73.           //左滑动  
  74.             if (e1.getX() - e2.getX() > 80||e1.getY()-e2.getY()>80) {    
  75.                   
  76.                 flag=(--flag+myClass.length)%myClass.length;  
  77.                 Intent intent=new Intent(getBaseContext(), myClass[flag]);  
  78.                 Log.d("flag",flag+"");  
  79.                 startActivity(intent);  
  80.                  
  81.                 return true;    
  82.             }   
  83.             //右滑动  
  84.             else if (e1.getX() - e2.getX() <-80||e1.getY()-e2.getY()<-80) {    
  85.               Log.i("Fling""baseactivity");         
  86.                 flag=++flag%myClass.length;  
  87.                 Intent intent=new Intent(getBaseContext(),myClass[flag]);  
  88.                 startActivity(intent);  
  89.                 Log.d("flag",flag+"");  
  90.                 return true;    
  91.             }    
  92.             return true;    
  93.     }  
  94.   
  95. }  

继承它的两个activity。分别如下:

GestureTestActivity引入了我们自定义的gesturelist

  1. GestureTestActivity  

  1. package com.gesturetest;  
  2.   
  3.   
  4.   
  5. import android.os.Bundle;  
  6. /** 
  7.  *  
  8.  * @author liwan 
  9.  *这里的GestureList是在xml布局的 
  10.  */  
  11. public class GestureTestActivity extends BaseActivity {  
  12.     /** 
  13.      * 自定义的listview 
  14.      */  
  15.     GestureList gestureListView;  
  16.     /** 
  17.      * 自定义的adapter 
  18.      */  
  19.     MyBaseAdapter myBaseAdapter;  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         myBaseAdapter=new MyBaseAdapter(this);  
  25.         gestureListView=(GestureList)this.findViewById(R.id.list1);  
  26.         gestureListView.setAdapter(myBaseAdapter);  
  27.     }  
  28. }  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="150dp"  
  10.         android:text="第一个activity,在这里滑动是调用activity的手势处理哦,在listview里面滑动式调用list自己的手势" />  
  11.   
  12.     <com.gesturetest.GestureList  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/list1">  
  16.     </com.gesturetest.GestureList>  
  17. </LinearLayout>  

TestActivity的代码如下。它引用了gesturelist,但是它是使用addview()引入的。
  1. package com.gesturetest;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.widget.LinearLayout;  
  6. /** 
  7.  * 这里的GestureList是用代码引入的,没有在xml布局 
  8.  * @author liwan 
  9.  * 
  10.  */  
  11. public class TestActivity extends BaseActivity {  
  12.     /** 
  13.      * 自定义的listview 
  14.      */  
  15.     GestureList gestureListView;  
  16.     /** 
  17.      * 自定义的adapter 
  18.      */  
  19.     MyBaseAdapter myBaseAdapter;  
  20.     LinearLayout linearLayout;  
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         // TODO Auto-generated method stub  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.test);  
  26.         linearLayout=(LinearLayout)this.findViewById(R.id.li);  
  27.         gestureListView =new GestureList(TestActivity.this);  
  28.         myBaseAdapter=new MyBaseAdapter(TestActivity.this);  
  29.         gestureListView.setAdapter(myBaseAdapter);  
  30.         linearLayout.addView(gestureListView);  
  31.           
  32.     }  
  33.   
  34. }  

  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.     android:id="@+id/li"  
  7. >  
  8.     <TextView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="100dp"  
  11.         android:text="第二个activity,在这里滑动是调用activity的手势处理哦,在listview里面滑动式调用list自己的手势" />  
  12. </LinearLayout>  


下面说下核心了,也就是我们的GestureList,由于它的重写,实现了它也可以捕获手势事件了。在onTouchEvent方法里,将处理交给手势监听器。

在xml布局里面使用GestureList,默认的会调用这个构造方法GestureList(Context context, AttributeSet attrs)。一定要重写这个构造方法。

代码如下:

  1. package com.gesturetest;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.GestureDetector;  
  7. import android.view.MotionEvent;  
  8. import android.widget.ListView;  
  9. /** 
  10.  *  
  11.  * @author liwan 
  12.  *自定义的listview,带有手势 
  13.  */  
  14. class GestureList extends ListView {  
  15.   
  16.     int flag=BaseActivity.flag;  
  17.     Context context;  
  18.     GestureDetector gestureDetector;  
  19.     /** 
  20.      * 在xml布局里面使用GestureList,默认的会调用这个构造方法 
  21.      * @param context 
  22.      * @param attrs 
  23.      */  
  24.     public GestureList(Context context, AttributeSet attrs) {  
  25.         super(context, attrs);  
  26.         // TODO Auto-generated constructor stub  
  27.         this.context=context;  
  28.         gestureDetector=new GestureDetector(context,new Gesture(context));  
  29.         Log.d("12","2");  
  30.     }  
  31.     public GestureList(Context context, AttributeSet attrs, int defStyle) {  
  32.         super(context, attrs, defStyle);  
  33.         // TODO Auto-generated constructor stub  
  34.         this.context=context;  
  35.         gestureDetector=new GestureDetector(context,new Gesture(context));  
  36.         Log.d("12","3");  
  37.     }  
  38.   
  39.     public GestureList(Context context) {  
  40.         super(context);  
  41.         // TODO Auto-generated constructor stub  
  42.         this.context=context;  
  43.         gestureDetector=new GestureDetector(context,new Gesture(context));  
  44.         Log.d("12","1");  
  45.     }  
  46.   
  47.     @Override  
  48.     public boolean onTouchEvent(MotionEvent ev) {  
  49.           
  50.         if(gestureDetector.onTouchEvent(ev)) return true;  
  51.         return super.onTouchEvent(ev);  
  52.     }  
  53.       
  54. }  

  1. package com.gesturetest;  
  2.   
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.util.Log;  
  6. import android.view.GestureDetector.OnGestureListener;  
  7. import android.view.MotionEvent;  
  8.   
  9.   
  10.   
  11. public class Gesture implements OnGestureListener{  
  12.     /** 
  13.      * 得到全局的标志位 
  14.      */  
  15.     int flag=BaseActivity.flag;  
  16.     @SuppressWarnings("rawtypes")  
  17.     /** 
  18.      * 得到activity数组 
  19.      */  
  20.     Class[] myClass=BaseActivity.myClass;  
  21.     Context context;  
  22.     public Gesture(Context context) {  
  23.         // TODO Auto-generated constructor stub  
  24.         this.context=context;  
  25.     }  
  26.     @Override  
  27.     public boolean onDown(MotionEvent e) {  
  28.         // TODO Auto-generated method stub  
  29.         return false;  
  30.     }  
  31.   
  32.     @Override  
  33.     public void onShowPress(MotionEvent e) {  
  34.         // TODO Auto-generated method stub  
  35.           
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onSingleTapUp(MotionEvent e) {  
  40.         // TODO Auto-generated method stub  
  41.         return false;  
  42.     }  
  43.   
  44.     @Override  
  45.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  46.             float distanceY) {  
  47.         // TODO Auto-generated method stub  
  48.         return false;  
  49.     }  
  50.   
  51.     @Override  
  52.     public void onLongPress(MotionEvent e) {  
  53.         // TODO Auto-generated method stub  
  54.           
  55.     }  
  56.   
  57.     @Override  
  58.     /** 
  59.      * 滑动事件的处理 
  60.      */  
  61.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  62.             float velocityY) {  
  63.           Log.i("Fling""baseactivity");    
  64.           //左滑动  
  65.             if (e1.getX() - e2.getX() > 80) {    
  66.                   
  67.                 flag=(--flag+myClass.length)%myClass.length;  
  68.               //改变BaseActivity,让其知道标志位改变了  
  69.                 BaseActivity.flag=flag;  
  70.                 Intent intent=new Intent(context, myClass[flag]);  
  71.                //需要context才能启动activity  
  72.                 context.startActivity(intent);  
  73.                   
  74.                 return true;    
  75.             }   
  76.             //右滑动  
  77.             else if (e1.getX() - e2.getX() <-80) {    
  78.               Log.i("Fling""baseactivity");         
  79.                 flag=++flag%myClass.length;  
  80.                 //改变BaseActivity,让其知道标志位改变了  
  81.                 BaseActivity.flag=flag;  
  82.                 Intent intent=new Intent(context,myClass[flag]);  
  83.                 //需要context才能启动activity  
  84.                 context.startActivity(intent);  
  85.                  
  86.                 return true;    
  87.             }    
  88.             return true;    
  89.     }  
  90.   
  91.               
  92.               
  93.               
  94.   
  95.           
  96.       
  97.   
  98.                       
  99.       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值