編輯:關於Android編程
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"1.0"
encoding=
"utf-8"
?>
大致是這樣子
加載中視圖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24"1.0"
encoding=
"utf-8"
?>
"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"
>
android:id=
"@+id/progressbar"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_centerVertical=
"true"
/>
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"
/>
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"1.0"
encoding=
"utf-8"
?>
"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"
>
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
>
android:id=
"@+id/progressbar"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_centerVertical=
"true"
/>
android:text=
"Loading…"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:layout_toRightOf=
"@+id/progressbar"
android:gravity=
"center"
/>
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:gravity=
"center_horizontal"
>
android:text=
"Finished!"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:layout_centerVertical=
"true"
/>
1.Toast源碼分析老規矩,我們先去看Toast的源碼。Toast有兩種顯示布局方式,一種最常見調用Toast.makeText() ,看源碼是這樣寫的pu
經常逛github,總看到別人的readme中寫著compile ‘com.xxx:1.0.xxx’,這個已經越來越普及,個人,團人,公司都在用,
引言我們在做 Android 開發時,常常需要實現異步加載圖片/網頁/其他。事實上,要實現異步加載,就需要實現線程間通信,而在 Android 中結合使用 Handler
上一篇關於Android中ListView的介紹講的是如何制作一個具有兩行文本的自定義控件,作為ListView的Item的使用方法。本文接下來也是圍繞ListView和