ViewSwitcher 的作用简单来说就是:在两个视图间转换时显示动画
它的两个子类应该很熟悉,ImageSwitcher:转换图片时增加动画效果; TextSwitcher: 转换文字时增加动画效果; 其实例见apidemos中ImageSwitcher实例和TextSwitcher实例
但不要忽略ViewSwicher,在一些场合还是很有用的
在android里视图切换是一个很常见的需求,比如说加载view和后台背景,当后台加载数据时,loding view显示,数据View隐藏,加载完成,反向此过程。使用ViewSwicher提供了简单的逻辑,产生更可读的代码。
举个最常见的例子,列表底部加载
Button more
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < Button xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:id = "@+id/btn_loadmorecontacts" |
04 | android:text = "Load More Items" |
05 | android:layout_width = "fill_parent" |
06 | android:layout_height = "wrap_content" |
07 | android:textAppearance = "?android:attr/textAppearanceLarge" |
08 | android:minHeight = "?android:attr/listPreferredItemHeight" |
09 | android:textColor = "#FFFFFF" |
10 | android:background = "<a href=" http://my.oschina.net/asia" rel = "nofollow" target = "_blank" >@android</ a > :drawable/list_selector_background" |
11 | android:clickable="true" |
12 | android:onClick="onClick" /> |
大致是这样子
加载中视图:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:layout_width = "wrap_content" |
04 | android:layout_height = "wrap_content" |
05 | android:gravity = "center_horizontal" |
06 | android:minHeight = "?android:attr/listPreferredItemHeight" > |
09 | android:id = "@+id/progressbar" |
10 | android:layout_width = "wrap_content" |
11 | android:layout_height = "wrap_content" |
12 | android:layout_centerVertical = "true" /> |
15 | android:text = "Loading…" |
16 | android:textAppearance = "?android:attr/textAppearanceLarge" |
17 | android:layout_height = "wrap_content" |
18 | android:layout_width = "wrap_content" |
19 | android:layout_toRightOf = "@+id/progressbar" |
20 | android:layout_centerVertical = "true" |
21 | android:gravity = "center" |
22 | android:padding = "10dip" |
23 | android:textColor = "#FFFFFF" /> |
01 | public class ViewSwitcherExample extends ListActivity |
02 | implements OnClickListener { |
05 | static final String[] ITEMS = new String[] |
06 | { "List Item 1" , "List Item 2" , |
07 | "List Item 3" , "List Item 4" , |
08 | "List Item 5" , "List Item 6" , |
09 | "List Item 7" , "List Item 8" , |
10 | "List Item 9" , "List Item 10" }; |
13 | private ViewSwitcher switcher; |
15 | /** Called when the activity is first created. */ |
17 | public void onCreate(Bundle savedInstanceState) { |
18 | super .onCreate(savedInstanceState); |
21 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
24 | switcher = new ViewSwitcher( this ); |
27 | Button footer = (Button)View.inflate( this , R.layout.btn_loadmore, null ); |
30 | View progress = View.inflate( this , R.layout.loading_footer, null ); |
33 | switcher.addView(footer); |
34 | switcher.addView(progress); |
37 | getListView().addFooterView(switcher); |
40 | setListAdapter( new ArrayAdapter( this , |
41 | android.R.layout.simple_list_item_1, ITEMS)); |
67 | protected void onPostExecute(Object result) { |
69 | switcher.showPrevious(); |
2 | switcher.showPrevious(); |
当然你也可以使用xml形式构造ViewSwicher,这里加上了系统自带的切换效果@android:anim/slide_in_left和@android:anim/slide_out_right
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < ViewSwitcher xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:id = "@+id/profileSwitcher" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
06 | android:inAnimation = "@android:anim/slide_in_left" |
07 | android:outAnimation = "@android:anim/slide_out_right" > |
09 | android:layout_width = "wrap_content" |
10 | android:layout_height = "wrap_content" > |
12 | android:id = "@+id/progressbar" |
13 | android:layout_width = "wrap_content" |
14 | android:layout_height = "wrap_content" |
15 | android:layout_centerVertical = "true" /> |
17 | android:text = "Loading…" |
18 | android:layout_height = "wrap_content" |
19 | android:layout_width = "wrap_content" |
20 | android:layout_toRightOf = "@+id/progressbar" |
21 | android:gravity = "center" /> |
25 | android:layout_width = "wrap_content" |
26 | android:layout_height = "wrap_content" |
27 | android:gravity = "center_horizontal" > |
29 | android:text = "Finished!" |
30 | android:layout_height = "wrap_content" |
31 | android:layout_width = "wrap_content" |
32 | android:layout_centerVertical = "true" /> |
你喜欢的话可以加入动画效果,使用View gone、Visible方式还是ViewSwicher还是看自己喜好了