ViewFlipper效果-------滑动切换页面

运行效果图


修改布局文件activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
<!-- 定义ViewFlipper视图切换器 -->
	<ViewFlipper 
	    android:id="@+id/ViewFlipper"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content">
<!-- 定义第一个屏幕 -->
	   <LinearLayout
	       android:id="@+id/LinearLayout01"
	       android:layout_width="match_parent"
	       android:layout_height="wrap_content"
	       android:orientation="vertical">
	       <TextView 
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:gravity="center"
	           android:text="登录页面"/>
	       <EditText 
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:hint="请输入用户名"/>
	       <EditText 
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	            android:password="true"
	           android:maxLength="12"
	           android:hint="请输入密码"/>
	       <Button
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:text="登录"/>
	   </LinearLayout>
<!-- 定义第二个屏幕 -->
	   <LinearLayout
	       android:id="@+id/LinearLayout02"
	       android:layout_width="match_parent"
	       android:layout_height="wrap_content"
	       android:orientation="vertical">
	       <TextView 
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:gravity="center"
	           android:text="注册页面"/>
	       <EditText 
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:hint="请输入用户名"/>
	       <EditText 
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	            android:password="true"
	           android:maxLength="12"
	           android:hint="请输入密码"/>
	       <EditText 
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:password="true"
	           android:maxLength="12"
	           android:hint="再次输入密码"/>
	       <Button
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:text="登录"/>
	   </LinearLayout>
<!-- 定义第二个屏幕 -->
	   <LinearLayout
	       android:id="@+id/LinearLayout03"
	       android:layout_width="match_parent"
	       android:layout_height="wrap_content"
	       android:orientation="vertical">
	       <TextView 
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:gravity="center"
	           android:text="登录成功"/>
	       <Button
	           android:layout_width="match_parent"
	           android:layout_height="wrap_content"
	           android:text="进入商城"/>
	   </LinearLayout>
	</ViewFlipper>
</LinearLayout>


修改MainActivity.java文件:


import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

public class Show_2Activity extends Activity implements android.view.GestureDetector.OnGestureListener{
	private ViewFlipper flipper;//定义页面中的ViewFlipper对象
	private GestureDetector detector;//定义手势识别监听器对象
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_2);
		setTitle("滑动切换屏幕");
		//初始化手势
		detector=new GestureDetector((android.view.GestureDetector.OnGestureListener) this);
		findById();//获取对象
	}

	private void findById() {
		flipper=(ViewFlipper) this.findViewById(R.id.ViewFlipper);//得到对象id
	}

	//当页面被触摸时自动回调,把拿到的时间给手势识别器进行处理
	public boolean onTouchEvent(MotionEvent event){
		return this.detector.onTouchEvent(event);
	}
	//当页面按下时自动回调哦
	public boolean onDown(MotionEvent e){
		return false;
	}
	//当页面滑动时自动回调
	public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){
		//得到滑动过程中的两点的X和Y坐标,进行判断
		if(e1.getX()>e2.getX()){
			//当向左滑动时显示上一张
			this.flipper.showNext();
		}else if(e1.getX()<e2.getX()){
			//党项右滑动时显示下一张
			this.flipper.showPrevious();
		}else{
			return false;
		}
		return true;
	}
	//当在界面长按是自动调用
	public void onLongPress(MotionEvent e){
	}
	//界面滚动的时候调用
	public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY){
		return false;
	}
	//当轻击界面的时候自动调用
	public boolean onSingleTapUp(MotionEvent e){
		return false;
	}
	//当界面被按压的时候自动调用
	public void onShowPress(MotionEvent e){
	}


}

可以添加动画

在res文件下新建anim文件,然后在新建布局文件in_leftright.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="3000"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />
</set>
依照上述再新建out_leftright.xml,in_rightleft.xml,out_rightleft.xml文件

调用

//得到滑动过程中的两点的X和Y坐标,进行判断
		if(e1.getX()>e2.getX()){
			flipper.setInAnimation(this, R.anim.in_rightleft);
			flipper.setOutAnimation(this, R.anim.out_rightleft);</strong>
			//当向左滑动时显示上一张
			this.flipper.showNext();//调用该函数来显示FrameLayout里面的下一个View。
		}else if(e1.getX()<e2.getX()){
			<strong>flipper.setInAnimation(this, R.anim.in_leftright);
			flipper.setOutAnimation(this, R.anim.out_leftright);
			//党项右滑动时显示下一张
			this.flipper.showPrevious();//调用该函数来显示FrameLayout里面的上一个View
		}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饕餮幻想家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值