使用 TabActivity 實現滑動翻頁(帶動畫)和標籤置底

來源:互聯網
上載者:User

先看效果

 其實做完後,才發現,TabActivity 並不難用,只需要你自己去擴充一些他的方法,就可以達到你自己想到效果。  不多說了,把實現動畫的部分貼出現,其他的自己看源碼吧。

@Overridepublic void setCurrentTab(int index) {  int mCurrentTabID = getCurrentTab();  if (null != getCurrentView()) {   // 第一次設定 Tab 時,該值為 null。   if (isOpenAnimation) {    if (mCurrentTabID == (mTabCount - 1) && index == 0) {     getCurrentView().startAnimation(slideLeftOut);    } else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {     getCurrentView().startAnimation(slideRightOut);    } else if (index > mCurrentTabID) {     getCurrentView().startAnimation(slideLeftOut);    } else if (index < mCurrentTabID) {     getCurrentView().startAnimation(slideRightOut);    }   }  }  super.setCurrentTab(index);  if (isOpenAnimation) {   if (mCurrentTabID == (mTabCount - 1) && index == 0) {    getCurrentView().startAnimation(slideLeftIn);   } else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {    getCurrentView().startAnimation(slideRightIn);   } else if (index > mCurrentTabID) {    getCurrentView().startAnimation(slideLeftIn);   } else if (index < mCurrentTabID) {    getCurrentView().startAnimation(slideRightIn);   }  }}

這個程式是Android2.2的。

不過是繼承了 TabHost 組件類,並擴充了其 setCurrentTab(int index) 方法,不過有一個 Bug 沒有解決,便當連續快速的滑動螢幕時,當 TabHost 載入的 view 或 activity 背景圖為透明效果時,會出現重影現象。

        希望有高手解決。

        關於標籤置底,其實可以查看 XML 檔案得到答案,使用 TabActivity 時,其布局檔案的頂級視圖必須為 TabHost 控制項,通過看 TabHost 的源碼,可以看到,它其實就是一個 FrameLayout,包含了兩個控制項:FrameLayout mTabContent(展示我們載入的 View 或 Activity)  和 TabWidget mTabWidget(展示 Tab 的標籤,其實就是一個 LinearLayout),預設布局都是採用系統的,所以我們可以在自己的
XML 檔案中,將這兩個的順序更換一下,就可以了,同樣的,既然我們可以得到 TabWidget,那麼就可以對其進行布局設計,制定效果也就可以實現了。

       現在想來,QQ、UC這些漂亮的按鈕滑動方式,會不會也是這樣實現的呢,研究中…………

完整代碼:http://download.csdn.net/detail/cpcpc/3600134

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.