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

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

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

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

举个最常见的例子,列表底部加载

Button more

01 <?xmlversion="1.0"encoding="utf-8"?>
02 <Buttonxmlns: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 <?xmlversion="1.0"encoding="utf-8"?>
02 <RelativeLayoutxmlns: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">
07
08 <ProgressBar
09 android:id="@+id/progressbar"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:layout_centerVertical="true"/>
13
14 <TextView
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"/>
24 </RelativeLayout>

01 publicclassViewSwitcherExampleextendsListActivity
02 implementsOnClickListener {
03
04 //sample list items
05 staticfinalString[] ITEMS =newString[]
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"};
11
12 //the ViewSwitcher
13 privateViewSwitcher switcher;
14
15 /** Called when the activity is first created. */
16 @Override
17 publicvoidonCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19
20 //no window title
21 requestWindowFeature(Window.FEATURE_NO_TITLE);
22
23 //create the ViewSwitcher in the current context
24 switcher =newViewSwitcher(this);
25
26 //底部 Button: see XML1
27 Button footer = (Button)View.inflate(this, R.layout.btn_loadmore,null);
28
29 //进度条View: see XML2
30 View progress = View.inflate(this, R.layout.loading_footer,null);
31
32 //向viewSwitcher加入view (first added will show first)
33 switcher.addView(footer);
34 switcher.addView(progress);
35
36 //listview底部加入swicher
37 getListView().addFooterView(switcher);
38
39 //add items to the ListView
40 setListAdapter(newArrayAdapter(this,
41 android.R.layout.simple_list_item_1, ITEMS));
42 }
43
44 @Override/* Load More Button Was Clicked */
45 public void onClick(View arg0) {
46 //first view is showing, show the second progress view
47 switcher.showNext();
48 //and start background work
49 new getMoreItems().execute();
50 }
51
52 /** Background Task To Get More Items**/
53 private class getMoreItems extends AsyncTask {
54 @Override
55 protected Object doInBackground(Void… params) {
56 //code to add more items
57 //...
58 try {
59 Thread.sleep(3000); //only to demonstrate
60 } catch (InterruptedException e) {
61 e.printStackTrace();
62 }
63 return null;
64 }
65
66 @Override /* Background Task is Done */
67 protectedvoidonPostExecute(Object result) {
68 //go back to the first view
69 switcher.showPrevious();
70 //update the ListView
71 }
72 }
73 }

1 switcher.showNext();// Switches to the next view
2 switcher.showPrevious();// Switches to the previous view

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

01 <?xmlversion="1.0"encoding="utf-8"?>
02 <ViewSwitcherxmlns: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">
08 <RelativeLayout
09 android:layout_width="wrap_content"
10 android:layout_height="wrap_content">
11 <ProgressBar
12 android:id="@+id/progressbar"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:layout_centerVertical="true"/>
16 <TextView
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"/>
22 </RelativeLayout>
23
24 <RelativeLayout
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:gravity="center_horizontal">
28 <TextView
29 android:text="Finished!"
30 android:layout_height="wrap_content"
31 android:layout_width="wrap_content"
32 android:layout_centerVertical="true"/>
33 </RelativeLayout>
34
35 </ViewSwitcher>

你喜欢的话可以加入动画效果,使用View gone、Visible方式还是ViewSwicher还是看自己喜好了

更多相关文章

  1. Android(安卓)ListView异步加载图片乱序问题,原因分析及解决方案
  2. Android(安卓)ProgressBar 自定义样式(一)
  3. Android之富有动感的底部弹窗效果
  4. Android(安卓)仿美团网,大众点评购买框悬浮效果之修改版
  5. Android(安卓)动画之帧动画
  6. android的xUtils框架
  7. Android(安卓)自定义Toast显示多种方式
  8. Android(安卓)MultiDex
  9. Android手把手教你实现卡片式瀑布流效果(RecyclerView+CardView,附

随机推荐

  1. INSERT INTO SELECT语句与SELECT INTO FR
  2. SQL Server中的执行引擎入门 图解
  3. 分析SQL语句性能3种方法分享
  4. sqlserver中重复数据值只取一条的sql语句
  5. SQL 特殊语句(学习笔记)
  6. sqlserver通用的删除服务器上的所有相同
  7. SQL Server修改标识列方法 如自增列的批
  8. Sql Server 索引使用情况及优化的相关Sql
  9. sqlserver中求字符串中汉字的个数的sql语
  10. sqlserver中根据字符分割字符串的最好的