ViewSwitcher使用

转载地址:http://blog.csdn.net/bigconvience/article/details/26168229

android.widget.ViewSwitcher是ViewAnimator的子类,用于在两个View之间切换,但每次只能显示一个View。

一个ViewSWitcher控件只包含两个子View控件,并且同一时刻只有一个显示。它在两者之间切换,并显示动画。通常会使用ImageSwitcher对象和TextSwitcher对象。它们都提供了设置子View的方法----------可绘制资源或者是文本字符串,然后将从当前显示状态以动画形式切换到新的内容。

ViewSwitcher的addView函数的代码如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * {@inheritDoc} 
  3.  * 
  4.  * @throws IllegalStateException if this switcher already contains two children 
  5.  */  
  6. @Override  
  7. public void addView(View child, int index, ViewGroup.LayoutParams params) {  
  8.     if (getChildCount() >= 2) {  
  9.         throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");  
  10.     }  
  11.     super.addView(child, index, params);  
  12. }  

可以看出,若View的数量超过两个,会抛出异常: java.lang.IllegalStateException,打印 "Can't add more than 2 views to a ViewSwitcher"  。你可以使用ViewSwitcher的factory创建View或添加自己创建的View。

下面用一个例子介绍一下ViewSwitcher的用法。

布局文件:activity_main.xml

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.               xmlns:tools="http://schemas.android.com/tools"  
  5.               android:layout_width="match_parent"  
  6.               android:layout_height="match_parent"  
  7.               android:orientation="vertical"  
  8.               tools:context=".MainActivity" >  
  9.   
  10.   
  11.     <LinearLayout  
  12.             android:layout_width="match_parent"  
  13.             android:layout_height="wrap_content"  
  14.             android:orientation="horizontal" >  
  15.   
  16.         <Button  
  17.                 android:id="@+id/prev"  
  18.                 android:layout_width="0dp"  
  19.                 android:layout_height="wrap_content"  
  20.                 android:layout_weight="1"  
  21.                 android:text="previous" />  
  22.   
  23.         <Button  
  24.                 android:id="@+id/next"  
  25.                 android:layout_width="0dp"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:layout_weight="1"  
  28.                 android:text="next" />  
  29.     </LinearLayout>  
  30.   
  31.     <ViewSwitcher  
  32.             android:id="@+id/viewswitcher"  
  33.             android:layout_width="match_parent"  
  34.             android:layout_height="wrap_content" >  
  35.   
  36.         <ImageView  
  37.                 android:layout_width="wrap_content"  
  38.                 android:layout_height="wrap_content"  
  39.                 android:src="@drawable/ic_launcher" />  
  40.   
  41.         <LinearLayout  
  42.                 android:layout_width="match_parent"  
  43.                 android:layout_height="wrap_content"  
  44.                 android:gravity="center"  
  45.                 android:orientation="vertical" >  
  46.   
  47.             <Button  
  48.                     android:layout_width="wrap_content"  
  49.                     android:layout_height="wrap_content"  
  50.                     android:text="- Button 2 -" />  
  51.   
  52.             <TextView  
  53.                     android:layout_width="wrap_content"  
  54.                     android:layout_height="wrap_content"  
  55.                     android:text="LinearLayout 2" />  
  56.         </LinearLayout>  
  57.     </ViewSwitcher>  
  58. </LinearLayout>  

Activity的代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.AndroidTest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.animation.Animation;  
  7. import android.view.animation.AnimationUtils;  
  8. import android.widget.Button;  
  9. import android.widget.ViewSwitcher;  
  10.   
  11. public class MyActivity extends Activity {  
  12.     Button buttonPrev, buttonNext;  
  13.     ViewSwitcher viewSwitcher;  
  14.   
  15.     Animation slide_in_left, slide_out_right;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.         buttonPrev = (Button) findViewById(R.id.prev);  
  23.         buttonNext = (Button) findViewById(R.id.next);  
  24.         viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);  
  25.   
  26.         slide_in_left = AnimationUtils.loadAnimation(this,  
  27.                 android.R.anim.slide_in_left);  
  28.         slide_out_right = AnimationUtils.loadAnimation(this,  
  29.                 android.R.anim.slide_out_right);  
  30.   
  31.         viewSwitcher.setInAnimation(slide_in_left);  
  32.         viewSwitcher.setOutAnimation(slide_out_right);  
  33.   
  34.         buttonPrev.setOnClickListener(new View.OnClickListener() {  
  35.   
  36.             @Override  
  37.             public void onClick(View arg0) {  
  38.                 viewSwitcher.showPrevious();  
  39.             }  
  40.         });  
  41.   
  42.         buttonNext.setOnClickListener(new View.OnClickListener() {  
  43.   
  44.             @Override  
  45.             public void onClick(View arg0) {  
  46.                 viewSwitcher.showNext();  
  47.             }  
  48.         });  
  49.         ;  
  50.     }  
  51. }  

实现效果图:



使用ViewSwitcher的setFactory设置切换的View,分为两步。

第一步:获得ViewSwithcer的实例

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

第二部:实现接口ViewFactory

  1.  switcher.setFactory(new ViewFactory()  
  2.         {  
  3.             @Override  
  4.             public View makeView()  
  5.             {  
  6.                 return inflater.inflate(R.layout.slidelistview, null);  
  7.             }  
  8.         });  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值