android ViewSwitcher的用法介绍

    ViewSwitcher 的作用简单来说就是:在两个视图间转换时显示动画

它的两个子类应该很熟悉,ImageSwitcher:转换图片时增加动画效果;TextSwitcher:转换文字时增加动画效果;其实例见apidemos中ImageSwitcher实例和TextSwitcher实例

但不要忽略ViewSwicher,在一些场合还是很有用的

在android里视图切换是一个很常见的需求,比如说加载view和后台背景,当后台加载数据时,loding view显示,数据View隐藏,加载完成,反向此过程。使用ViewSwicher提供了简单的逻辑,产生更可读的代码。

举个最常见的例子,模拟点击LoadMoreItems按钮或得更多数据。

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version= "1.0" encoding= "utf-8" ?> 
     android:id= "@+id/btn_loadmorecontacts"
     android:text= "Load More Items"
     android:layout_width= "fill_parent"
     android:layout_height= "wrap_content"
     android:textAppearance= "?android:attr/textAppearanceLarge"
     android:minHeight= "?android:attr/listPreferredItemHeight"
     android:textColor= "#FFFFFF"
     android:background= "@android:drawable/list_selector_background"
     android:clickable= "true"
     android:onClick= "onClick" />

大致是这样子

 


 

加载中视图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version= "1.0" encoding= "utf-8" ?> 
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:gravity= "center_horizontal"
     android:minHeight= "?android:attr/listPreferredItemHeight"
     
     <ProgressBar  
         android:id= "@+id/progressbar"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:layout_centerVertical= "true" /> 
     
     <TextView  
         android:text= "Loading…"
         android:textAppearance= "?android:attr/textAppearanceLarge"
         android:layout_height= "wrap_content"
         android:layout_width= "wrap_content"
         android:layout_toRightOf= "@+id/progressbar"
         android:layout_centerVertical= "true"
         android:gravity= "center"
         android:padding= "10dip"
         android:textColor= "#FFFFFF" /> 
</RelativeLayout>

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class ViewSwitcherExample extends ListActivity 
                  implements OnClickListener { 
         
     //sample list items 
     static final String[] ITEMS = new String[] 
           { "List Item 1" , "List Item 2" ,  
             "List Item 3" , "List Item 4" ,  
             "List Item 5" , "List Item 6" ,  
             "List Item 7" , "List Item 8" ,  
             "List Item 9" , "List Item 10" }; 
         
     //the ViewSwitcher 
     private ViewSwitcher switcher; 
         
     /** Called when the activity is first created. */
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
       super .onCreate(savedInstanceState); 
           
       //no window title 
       requestWindowFeature(Window.FEATURE_NO_TITLE); 
           
       //create the ViewSwitcher in the current context 
       switcher = new ViewSwitcher( this ); 
           
       //footer Button: see XML1 
       Button footer = (Button)View.inflate( this , R.layout.btn_loadmore, null ); 
           
       //progress View: see XML2 
       View progress = View.inflate( this , R.layout.loading_footer, null ); 
           
       //add the views (first added will show first) 
       switcher.addView(footer); 
       switcher.addView(progress); 
           
       //add the ViewSwitcher to the footer 
       getListView().addFooterView(switcher); 
           
       //add items to the ListView 
       setListAdapter( new ArrayAdapter( this
               android.R.layout.simple_list_item_1, ITEMS)); 
    
     
     @Override /* Load More Button Was Clicked */
     public void onClick(View arg0) { 
         //first view is showing, show the second progress view 
         switcher.showNext(); 
         //and start background work 
         new getMoreItems().execute(); 
    
         
     /** Background Task To Get More Items**/
     private class getMoreItems extends AsyncTask { 
         @Override 
         protected Object doInBackground(Void… params) { 
             //code to add more items 
             //... 
             try
                 Thread.sleep(3000); //only to demonstrate 
             } catch (InterruptedException e) { 
                 e.printStackTrace(); 
            
             return null
        
     
         @Override /* Background Task is Done */
         protected void onPostExecute(Object result) { 
             //go back to the first view 
             switcher.showPrevious(); 
                         //update the ListView 
        
    
}

当然你也可以使用xml形式构造ViewSwicher,这里加上了系统自带的切换效果@android:anim/slide_in_left和@android:anim/slide_out_right

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xmlversion= "1.0" encoding= "utf-8" ?> 
<ViewSwitcherxmlns:android= "http://schemas.android.com/apk/res/android"
    
android:id= "@+id/profileSwitcher"
android:layout_width= "fill_parent"
    
android:layout_height= "fill_parent"
android:inAnimation= "@android:anim/slide_in_left"
    
android:outAnimation= "@android:anim/slide_out_right"
<RelativeLayout 
    
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
    
<ProgressBar 
android:id= "@+id/progressbar"
    
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
    
android:layout_centerVertical= "true" /> 
<TextView 
    
android:text= "Loading…"
android:layout_height= "wrap_content"
    
android:layout_width= "wrap_content"
android:layout_toRightOf= "@+id/progressbar"
    
android:gravity= "center" /> 
</RelativeLayout> 
    
     
<RelativeLayout 
    
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
    
android:gravity= "center_horizontal"
<TextView 
    
android:text= "Finished!"
android:layout_height= "wrap_content"
    
android:layout_width= "wrap_content"
android:layout_centerVertical= "true" /> 
    
</RelativeLayout> 
     
    
</ViewSwitcher>
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分别用于图片和文本切换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值