Android ViewSwitcher简介和使用



Android ViewSwitcher简介和使用

Android ViewSwitcher主要应用场景之一:比如在一个布局文件中,根据业务需求,需要在两个View间切换,在任意一个时刻,只能显示一个View。
典型的应用比如一些社交类APP的标题栏,在分享照片之前,标题栏显示“拍照”按钮,用户拍完照后,接下来的动作是发送这张照片,那么合理的做法就是将标题栏的动作按钮变成“发送”按钮。
针对此种情况,相对比较简陋的做法是:可以考虑在标题栏事先存放两个按钮:拍照,发送。当前是“拍照”时,则将“发送”按钮设置为GONE;反之,当前是“发送”时,则将“拍照”按钮设置为GONE。
但是,相对更好的做法是使用ViewSwitcher。ViewSwitcher,故名思议,就是为了完成View之间的Switch。
现在给出一个完整示例代码例子说明。


测试的MainActivity.java :

package zhangphil.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		Button buttonPrev = (Button) findViewById(R.id.prev);
		Button buttonNext = (Button) findViewById(R.id.next);

		final ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);

		Animation slide_in_left = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
		Animation slide_out_right = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

		viewSwitcher.setInAnimation(slide_in_left);
		viewSwitcher.setOutAnimation(slide_out_right);

		buttonPrev.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// viewSwitcher.showPrevious();切换效果类似
				viewSwitcher.setDisplayedChild(0);
			}
		});

		buttonNext.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// viewSwitcher.showNext();切换效果类似
				viewSwitcher.setDisplayedChild(1);
			}
		});
	}
}


MainActivity.java需要的布局文件activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="前" />

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="后" />
    </LinearLayout>

    <ViewSwitcher
        android:id="@+id/viewSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="线性布局的TextView" />
        </LinearLayout>
    </ViewSwitcher>

</LinearLayout>


ViewFlipperViewSwitcher使用:屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。 通过查看OPhone API文档可以发现,有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。该类有如下几个和动画相关的函数: l setInAnimation:设置View进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation.Animation;一个接受两个参数,类型为Context和int,分别为Context对象和定义Animation的resourceID。 setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。 showNext: 调用该函数来显示FrameLayout里面的下一个View。 showPrevious:调用该函数来显示FrameLayout里面的上一个View。 一般不直接使用ViewAnimator而是使用它的两个子类ViewFlipperViewSwitcherViewFlipper可以用来指定FrameLayout内多个View之间的切换效果,可以一次指定也可以每次切换的时候都指定单独的效果。该类额外提供了如下几个函数: isFlipping: 用来判断View切换是否正在进行 setFilpInterval:设置View之间切换的时间间隔 startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行 stopFlipping: 停止View切换 ViewSwitcher 顾名思义Switcher特指在两个View之间切换。可以通过该类指定一个ViewSwitcher.ViewFactory 工程类来创建这两个View。该类也具有两个子类ImageSwitcher、TextSwitcher分别用于图片和文本切换。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangphil

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

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

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

打赏作者

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

抵扣说明:

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

余额充值