1.簡(jiǎn)介
????????? 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。
?????????? 簡(jiǎn)單的ViewAnimator 可以在兩個(gè)或兩個(gè)以上的視圖間實(shí)現(xiàn)動(dòng)畫效果,但是一次只能顯示一個(gè)子類。如果設(shè)置的話,子類可以按照一定規(guī)律來(lái)顯示。
?
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"> <!-- 滑動(dòng)翻頁(yè)顯示 --> <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中有三個(gè)線性布局,分別為初始加載和加載后以及加載失敗。當(dāng)我們?cè)赼ctivity中這個(gè)viewflipper實(shí)例化時(shí),我們打印
mViewFlipper.getDisplayedChild()
?
會(huì)得到3。說(shuō)明三個(gè)linnerlayout為她的子類。同時(shí)為了怕給主線程增加負(fù)擔(dān),我們采用異步實(shí)現(xiàn)。
??
private class NewsTask extends AsyncTask<Void, WSError, Album[]> { @Override public void onPreExecute() { mViewFlipper.setDisplayedChild(0); System.out.println("子類數(shù)目----"+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); }
?
這個(gè)類實(shí)現(xiàn)了我們的操作。
?在oncreate()中使用
new NewsTask().execute((Void)null);
?
調(diào)用異步操作。
接下來(lái),我們只講解子類1,即加載成功后的顯示效果。
如圖:
?在以上代碼中,if(albums != null && albums.length > 0){
mViewFlipper.setDisplayedChild(1);//設(shè)置顯示第一面即成功加載后的效果
ImageAdapter albumsAdapter = new ImageAdapter(HomeActivity.this);//自定義了一個(gè)adpter,來(lái)組織顯示樣式
??? albumsAdapter.setList(albums);//將數(shù)據(jù)放入其中,那么albums?自然是個(gè)bean了。里面定義了albums?的各種屬性,如發(fā)行時(shí)間,專輯名等。
??? mGallery.setAdapter(albumsAdapter);//給畫布設(shè)置適配器,使畫布顯示出效果來(lái)。
mGallery.setOnItemClickListener(mGalleryListener);//添加監(jiān)聽(tīng),實(shí)現(xiàn)對(duì)指定專輯的使用
??? mGallery.setSelection(albums.length/2, true);} //設(shè)置默認(rèn)顯示區(qū)域
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
