1.簡介
????????? ViewFlipper extends ViewAnimator,Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval。
?????????? 簡單的ViewAnimator 可以在兩個或兩個以上的視圖間實現動畫效果,但是一次只能顯示一個子類。如果設置的話,子類可以按照一定規律來顯示。
?
2. 具體使用
?邊上代碼邊解釋
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff">
<!-- 滑動翻頁顯示 -->
<ViewFlipper
android:orientation="vertical" android:id="@+id/ViewFlipper"
android:layout_width="fill_parent" android:layout_height="75dip"
android:background="@drawable/gradient_dark_purple">
<!-- (0) Loading -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_marginLeft="15dip" android:gravity="left|center_vertical">
<com.teleca.jamendo.widget.ProgressBar
android:id="@+id/ProgressBar" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.teleca.jamendo.widget.ProgressBar>
</LinearLayout>
<!-- (1) Gallery -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center">
<Gallery android:id="@+id/Gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:spacing="0px" />
</LinearLayout>
<!-- (2) Failure -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_marginLeft="15dip" android:gravity="left|center_vertical">
<com.teleca.jamendo.widget.FailureBar
android:id="@+id/FailureBar" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.teleca.jamendo.widget.FailureBar>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
?在viewflipper中有三個線性布局,分別為初始加載和加載后以及加載失敗。當我們在activity中這個viewflipper實例化時,我們打印
mViewFlipper.getDisplayedChild()
?
會得到3。說明三個linnerlayout為她的子類。同時為了怕給主線程增加負擔,我們采用異步實現。
??
private class NewsTask extends AsyncTask<Void, WSError, Album[]> {
@Override
public void onPreExecute() {
mViewFlipper.setDisplayedChild(0);
System.out.println("子類數目----"+mViewFlipper.getChildCount()+" "+mViewFlipper.getDisplayedChild());
mProgressBar.setText(R.string.loading_news);
super.onPreExecute();
}
@Override
public Album[] doInBackground(Void... params) {
JamendoGet2Api server = new JamendoGet2ApiImpl();
Album[] albums = null;
try {
albums = server.getPopularAlbumsWeek();
} catch (JSONException e) {
e.printStackTrace();
} catch (WSError e){
publishProgress(e);
}
System.out.println("doInBackground"+mViewFlipper.getDisplayedChild());
return albums;
}
@Override
public void onPostExecute(Album[] albums) {
if(albums != null && albums.length > 0){
mViewFlipper.setDisplayedChild(1);
ImageAdapter albumsAdapter = new ImageAdapter(HomeActivity.this);
albumsAdapter.setList(albums);
mGallery.setAdapter(albumsAdapter);
mGallery.setOnItemClickListener(mGalleryListener);
mGallery.setSelection(albums.length/2, true); // animate to center
} else {
mViewFlipper.setDisplayedChild(2);
mFailureBar.setOnRetryListener(new OnClickListener(){
@Override
public void onClick(View v) {
new NewsTask().execute((Void)null);
}
});
mFailureBar.setText(R.string.connection_fail);
}
System.out.println("onPostExecute"+mViewFlipper.getDisplayedChild());
super.onPostExecute(albums);
}
?
這個類實現了我們的操作。
?在oncreate()中使用
new NewsTask().execute((Void)null);
?
調用異步操作。
接下來,我們只講解子類1,即加載成功后的顯示效果。
如圖:
?在以上代碼中,if(albums != null && albums.length > 0){
mViewFlipper.setDisplayedChild(1);//設置顯示第一面即成功加載后的效果
ImageAdapter albumsAdapter = new ImageAdapter(HomeActivity.this);//自定義了一個adpter,來組織顯示樣式
??? albumsAdapter.setList(albums);//將數據放入其中,那么albums?自然是個bean了。里面定義了albums?的各種屬性,如發行時間,專輯名等。
??? mGallery.setAdapter(albumsAdapter);//給畫布設置適配器,使畫布顯示出效果來。
mGallery.setOnItemClickListener(mGalleryListener);//添加監聽,實現對指定專輯的使用
??? mGallery.setSelection(albums.length/2, true);} //設置默認顯示區域
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

