android ViewSwitcher详解 .
来源:互联网 发布:windows xp 安装盘 编辑:程序博客网 时间:2024/05/22 02:29
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"
>
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
public
class
ViewSwitcherExample
extends
ListActivity
02
implements
OnClickListener {
03
04
//sample list items
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"
};
11
12
//the ViewSwitcher
13
private
ViewSwitcher switcher;
14
15
/** Called when the activity is first created. */
16
@Override
17
public
void
onCreate(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 =
new
ViewSwitcher(
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(
new
ArrayAdapter(
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
protected
void
onPostExecute(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
<?
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"
>
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还是看自己喜好了
- android ViewSwitcher详解
- android ViewSwitcher详解 .
- android ViewSwitcher详解
- Android ViewSwitcher
- Android ViewSwitcher组件
- Android ViewSwitcher 简单介绍
- Android ViewSwitcher、TextSwitcher、ImageSwitcher
- Android ViewSwitcher类
- ViewSwitcher
- ViewSwitcher
- Android----ViewFlipper,ViewSwitcher(ImageSwitcher,TextSwitcher)
- android中ViewSwitcher的使用
- android ViewSwitcher的用法介绍
- Android ViewSwitcher简介和使用
- Android中viewSwitcher的使用
- Android ViewSwitcher简介和使用
- Android ViewSwitcher 视图切换器
- Android界面编程之ViewSwitcher
- ios开发ASIHTTPRequest-进度追踪
- Array Performance Comparison: C-Array vs CCArray vs NSArray vs NSMutableArray
- 随机函数项目三
- HOJ 1002 A+B+C
- ios开发之ASIHTTPRequest-下载数据
- android ViewSwitcher详解 .
- marquee字体滚动
- paypal分享:外贸发货避免海关被扣之技巧
- Tomcat性能参数设置
- Oracle 10g 修改主机名或者IP解决EM无法重启
- 修改表的关键字
- hdu 4436 后缀数组
- ios开发之ASIHttpRequest-发送数据
- HOJ 1008 How many N