滑动切换界面---单Activity

PS:该方式,只是Activity里面的layout在切换,Activity始终只有一个MainActivity,∴ 控件在MainActivity中都可以得到,因此 不涉及 Activity间传值的问题。

 

一、检测 手指的滑动

1、

实现 Activity 的 onTouchEvent 事件:

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		return super.onTouchEvent(event);
	}

 

2、

2.1、实现接口 android.view.GestureDetector.OnGestureListener

public class MainActivity extends Activity implements android.view.GestureDetector.OnGestureListener

2.2、实现 该接口的 6个函数

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

 这里,别的 函数都不用管,只要关注 方法onFling 就可以了。

 

 3、编程

3.1、MainActivity 添加私有变量:

private GestureDetector FgestureDetector;

3.2、MainActivity 的 onCreate 中创建 GestureDetector对象:

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		FgestureDetector = new GestureDetector(MainActivity.this, this);
	}

3.3、MainActivity 的 onTouchEvent 中调用GestureDetector.onTouchEvent :

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		return FgestureDetector.onTouchEvent(event);
		//return super.onTouchEvent(event);
	}

3.4、onFling 的实现改成如下:

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
	{
		float fDelta = 120;
		if (e2.getX() - e1.getX() > fDelta)
		{
			//new AlertDialog.Builder(this).setTitle("Android 提示").setMessage("==> ==>").show();
			Toast.makeText(getApplication(), "==> ==>", Toast.LENGTH_LONG).show();
			
			return true;
		}
		else if (e2.getX() - e1.getX() < (-fDelta))
		{
			//new AlertDialog.Builder(this).setTitle("Android 提示").setMessage("<== <==").show();
			Toast.makeText(getApplication(), "<== <==", Toast.LENGTH_LONG).show();
			
			return true;
		}
		return true;
		//return false;
	}

 

 4、这样就能检测到 手指的滑动了。

 

 二、页面的切换:

1、MainActivity 中添加 变量:

private ViewFlipper Fvf01;

2、MainActivity 的 onCreate事件中创建 ViewFlipper 对象

Fvf01 = (ViewFlipper) this.findViewById(R.id.viewFlipper1);

3、MainActivity 的onTouchEvent事件中添加代码,使之变成如下形式:

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		Fvf01.stopFlipping();
		Fvf01.setAutoStart(false);	
		return FgestureDetector.onTouchEvent(event);
	}

4、MainActivity 的 onFling事件中添加代码,使之变成如下形式:

 1 @Override
 2     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
 3     {
 4         float fDelta = 120;
 5         if (e2.getX() - e1.getX() > fDelta)
 6         {
 7             //new AlertDialog.Builder(this).setTitle("Android 提示").setMessage("==> ==>").show();
 8             Toast.makeText(getApplication(), "==> ==>", Toast.LENGTH_LONG).show();
 9              
10             Animation rInAnim = AnimationUtils.loadAnimation(this, R.anim.push_right_in);
11             Animation rOutAnim = AnimationUtils.loadAnimation(this, R.anim.push_right_out);
12      
13             Fvf01.setInAnimation(rInAnim);
14             Fvf01.setOutAnimation(rOutAnim);
15             Fvf01.showPrevious();
16             return true;
17         }
18         else if (e2.getX() - e1.getX() < (-fDelta))
19         {
20             //new AlertDialog.Builder(this).setTitle("Android 提示").setMessage("<== <==").show();
21             Toast.makeText(getApplication(), "<== <==", Toast.LENGTH_LONG).show();
22              
23             Animation lInAnim = AnimationUtils.loadAnimation(this, R.anim.push_left_in);
24             Animation lOutAnim = AnimationUtils.loadAnimation(this, R.anim.push_left_out);
25      
26             Fvf01.setInAnimation(lInAnim);
27             Fvf01.setOutAnimation(lOutAnim);
28             Fvf01.showNext();
29             return true;
30         }
31         //return true;
32         return false;
33     }

 

5、项目-->res -->新建文件夹"anim" --> 里面放入4个xml文件:

  push_left_in.xml、push_left_out.xml、push_right_in.xml、push_right_out.xml

它们的内容分别为:

5.1、push_left_in.xml :

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3     <translate
 4         android:duration="500"
 5         android:fromXDelta="100%p"
 6         android:toXDelta="0" />
 7 
 8     <alpha
 9         android:duration="500"
10         android:fromAlpha="0.1"
11         android:toAlpha="1.0" />
12 </set>

5.2、push_left_out.xml :

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3     <translate
 4         android:duration="500"
 5         android:fromXDelta="0"
 6         android:toXDelta="-100%p" />
 7 
 8     <alpha
 9         android:duration="500"
10         android:fromAlpha="1.0"
11         android:toAlpha="0.1" />
12 </set>

5.3、push_right_in.xml :

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3     <translate
 4         android:duration="500"
 5         android:fromXDelta="-100%p"
 6         android:toXDelta="0" />
 7 
 8     <alpha
 9         android:duration="500"
10         android:fromAlpha="0.1"
11         android:toAlpha="1.0" />
12 </set>

5.4、push_right_out.xml :

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3     <translate
 4         android:duration="500"
 5         android:fromXDelta="0"
 6         android:toXDelta="100%p" />
 7 
 8     <alpha
 9         android:duration="500"
10         android:fromAlpha="1.0"
11         android:toAlpha="0.1" />
12 </set>

 

 三、修改界面layout

1、在 项目-->res-->layout 目录下,新建2个界面xml文件:

  zc_layout_1.xml、zc_layout_2.xml

内容为:

1.1、zc_layout_1.xml :

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <TextView
 7         android:id="@+id/textView1"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_alignParentRight="true"
11         android:layout_alignParentTop="true"
12         android:text="zc_layout_1" />
13 
14     <EditText
15         android:id="@+id/editText1"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_alignParentLeft="true"
19         android:layout_alignParentRight="true"
20         android:layout_below="@+id/textView1"
21         android:ems="10"
22         android:gravity="center_vertical|top"
23         android:inputType="textMultiLine"
24         android:minLines="10" >
25 
26         <requestFocus />
27     </EditText>
28 
29 
30 </RelativeLayout>

1.2、zc_layout_2.xml :

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <TextView
 7         android:id="@+id/textView1"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_alignParentLeft="true"
11         android:layout_alignParentTop="true"
12         android:text="zc_layout_2" />
13 
14 
15 
16 </RelativeLayout>

 

2、

原来的 activity_main.xml 内容为:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.slideandroidz.MainActivity" >
10 
11     <TextView
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:text="@string/hello_world" />
15 
16 </RelativeLayout>

现在 修改为:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.slideandroidz.MainActivity" >
10 
11     <ViewFlipper
12         android:id="@+id/viewFlipper1"
13         android:layout_width="match_parent"
14         android:layout_height="match_parent" >
15 
16         <include layout="@layout/zc_layout_1" />
17 
18         <include layout="@layout/zc_layout_2" />
19         
20     </ViewFlipper>
21 
22 </RelativeLayout>

这样修改后, activity_main.xml 就不负责显示具体的控件了,它只是用来负责容纳 zc_layout_1和zc_layout_2。

然后切换的时候,是 zc_layout_1和zc_layout_2 在切换,activity_main.xml 并不参与其中。

 

100、

我的代码 保存于:百度云 -->codeskill33 -->“全部文件 > Code__Demo_Test > Android手指滑动_切换界面_单Activity” --> SlideAndroidZ

 

 

PS:完整的代码的例子:

  1 package com.example.commandandroidz;
  2 
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.view.GestureDetector;
  6 import android.view.Menu;
  7 import android.view.MenuItem;
  8 import android.view.MotionEvent;
  9 import android.view.animation.Animation;
 10 import android.view.animation.AnimationUtils;
 11 import android.widget.ViewFlipper;
 12 
 13 public class MainActivity extends Activity implements android.view.GestureDetector.OnGestureListener
 14 {
 15 
 16     @Override
 17     protected void onCreate(Bundle savedInstanceState)
 18     {
 19         super.onCreate(savedInstanceState);
 20         setContentView(R.layout.activity_main);
 21         
 22         
 23         FgestureDetector = new GestureDetector(MainActivity.this, this);
 24         Fvf01 = (ViewFlipper) this.findViewById(R.id.viewFlipper1);
 25     }
 26 
 27     @Override
 28     public boolean onCreateOptionsMenu(Menu menu) {
 29         // Inflate the menu; this adds items to the action bar if it is present.
 30         getMenuInflater().inflate(R.menu.main, menu);
 31         return true;
 32     }
 33 
 34     @Override
 35     public boolean onOptionsItemSelected(MenuItem item) {
 36         // Handle action bar item clicks here. The action bar will
 37         // automatically handle clicks on the Home/Up button, so long
 38         // as you specify a parent activity in AndroidManifest.xml.
 39         int id = item.getItemId();
 40         if (id == R.id.action_settings) {
 41             return true;
 42         }
 43         return super.onOptionsItemSelected(item);
 44     }
 45 
 46 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
 47 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
 48     
 49     @Override
 50     public boolean onTouchEvent(MotionEvent event)
 51     {
 52         Fvf01.stopFlipping();
 53         Fvf01.setAutoStart(false); 
 54         return FgestureDetector.onTouchEvent(event);
 55         //return super.onTouchEvent(event);
 56     }
 57     
 58     // *** *** ***
 59     
 60     private GestureDetector FgestureDetector;
 61     private ViewFlipper Fvf01;
 62     
 63     @Override
 64     public boolean onDown(MotionEvent e)
 65     {return false;}
 66      
 67     @Override
 68     public void onShowPress(MotionEvent e)
 69     {}
 70      
 71     @Override
 72     public boolean onSingleTapUp(MotionEvent e)
 73     {return false;}
 74      
 75     @Override
 76     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
 77     {return false;}
 78      
 79     @Override
 80     public void onLongPress(MotionEvent e)
 81     {}
 82      
 83     @Override
 84     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
 85     {
 86         float fDelta = 120;
 87         if (e2.getX() - e1.getX() > fDelta)
 88         {
 89             //new AlertDialog.Builder(this).setTitle("Android 提示").setMessage("==> ==>").show();
 90             //Toast.makeText(getApplication(), "==> ==>", Toast.LENGTH_LONG).show();
 91              
 92             Animation rInAnim = AnimationUtils.loadAnimation(this, R.anim.push_right_in);
 93             Animation rOutAnim = AnimationUtils.loadAnimation(this, R.anim.push_right_out);
 94      
 95             Fvf01.setInAnimation(rInAnim);
 96             Fvf01.setOutAnimation(rOutAnim);
 97             Fvf01.showPrevious();
 98             return true;
 99         }
100         else if (e2.getX() - e1.getX() < (-fDelta))
101         {
102             //new AlertDialog.Builder(this).setTitle("Android 提示").setMessage("<== <==").show();
103             //Toast.makeText(getApplication(), "<== <==", Toast.LENGTH_LONG).show();
104              
105             Animation lInAnim = AnimationUtils.loadAnimation(this, R.anim.push_left_in);
106             Animation lOutAnim = AnimationUtils.loadAnimation(this, R.anim.push_left_out);
107      
108             Fvf01.setInAnimation(lInAnim);
109             Fvf01.setOutAnimation(lOutAnim);
110             Fvf01.showNext();
111             return true;
112         }
113         //return true;
114         return false;
115     }
116 }

 

PS:

 1         EditText edtMsg01 = (EditText)findViewById(R.id.editMsg01);
 2         edtMsg01.setOnTouchListener(new View.OnTouchListener()
 3         {
 4             @Override
 5             public boolean onTouch(View v, MotionEvent event)
 6             {
 7                 Fvf01.stopFlipping();
 8                 Fvf01.setAutoStart(false); 
 9                 return FgestureDetector.onTouchEvent(event);
10                 //return false;
11             }
12         });

 

B

 

posted @ 2015-11-06 08:55  codeskill_android  阅读(285)  评论(0编辑  收藏  举报