注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。
原文鏈接: http://developer.android.com/training/basics/fragments/fragment-ui.html
當你在設計你的應用時,為了支持不同的屏幕尺寸,你可以在不同的布局配置中重用你的fragment,以此在可用的屏幕空間上獲得最優化的用戶體驗。
例如,在一個手持設備上,以單一窗格每次只顯示一個fragment也許是一個不錯的選擇。相對應的,你也許希望在屏幕更大的平板設備上并排顯示多個fragment,為用戶顯示更多的信息。
圖1. 兩個fragment,在不同屏幕尺寸上隸屬于同一個activity的顯示效果。在大屏幕上,兩個fragment可以同時并排顯示。但在手持設備上,同一時間只能容納一個fragment,所以當用戶進行操作時,必須令fragment相互替換。
FragmentManager 類提供了允許你在運行時為一個activity添加,刪除和替換fragments的方法,以此來創建一個動態的用戶體驗。
?
一). 在運行時為一個activity添加一個fragment
與上一節課中在XML布局文件內通過 <fragment>標簽 為一個activity定義一個fragment有所不同,你可以在運行時為一個activity添加一個fragment。如果你計劃著在activity的生命周期過程中改變fragment,那么這么做是必要的。
為了實現諸如添加或刪除一個fragment的事務,你必須使用 FragmentManager 來創建一個 FragmentTransaction ,它提供了添加,刪除,替換fragment的APIs,同時還有其他fragment相關的事務。
如果你的activity允許fragments可以被刪除或者替換,你應該在activity的 onCreate() 方法中,將初始化好的fragment添加至activity。
一個處理fragment時(尤其是你在運行時添加一個fragment)的關鍵的規則是:該fragment必須在布局中有一個 View 容器,fragment的布局將會放置于其中。
下面的布局是上一節課中所展示的布局的另一個形式,它在同一時刻只顯示一個fragment。為了將一個fragment替換成另外一個,這個activity的布局包含了一個空的 FrameLayout ,它的作用相當于一個fragment容器。
注意到這里的文件名和上一節課中的那個例子是一樣的,但是布局文件的目錄路徑中不包含“ large ”這一適配符,所以當當設備的屏幕比“ large ”這一規格要小時(此時屏幕無法同時裝下兩個fragment),這個布局就會被使用。
res/layout/news_articles.xml:
< FrameLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:id ="@+id/fragment_container" android:layout_width ="match_parent" android:layout_height ="match_parent" />
在你的Activity中,如果使用的是 Support Library APIs,可以 調用 getSupportFragmentManager() 來獲得一個 FragmentManager 。之后調用 beginTransaction() 來創建一個 FragmentTransaction ,然后調用 add() 來添加一個fragment。
你可以通過使用相同的 FragmentTransaction ,來為這個activity執行多個fragment事務。當你決定要做出這樣的改變,你必須在最后執行 commit() 。
例如:這是一個如何添加一個fragment至之前的布局的例子:
import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.news_articles); // Check that the activity is using the layout version with // the fragment_container FrameLayout if (findViewById(R.id.fragment_container) != null ) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null ) { return ; } // Create a new Fragment to be placed in the activity layout HeadlinesFragment firstFragment = new HeadlinesFragment(); // In case this activity was started with special instructions from an // Intent, pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } } }
因為這個fragment已經在運行時被添加至 FrameLayout 這一容器,而不是在activity的布局中通過 <fragment>標簽進行定義的,所以activity可以用一個不同的fragment去替換它。
?
二).?用一個不同的fragment進行替換
替換一個fragment的過程和添加一個基本類似,區別在于需要的是 replace() 方法而不是 add() 方法。
記住當你執行一個fragment事務(比如替換或刪除)時,最好允許用戶可以進行撤銷操作。為了實現用戶的撤銷,你必須在提交
FragmentTransaction
之前,執行
addToBackStack()
方法。
Note:
當你刪除或者替換了一個fragment,然后將這個事務添加至后退棧(back stack),被刪除的fragment會被停止(不是被銷毀)。如果用戶執行后退來恢復這個fragment,它會重新啟動。如果你不將這個事務添加至后退棧,這個fragment會在被替換或被刪除時直接被銷毀。
一個替換fragment的例子:
// Create fragment and give it an argument specifying the article it should show ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack( null ); // Commit the transaction transaction.commit();
addToBackStack() 方法接受一個可選的string參數,它用來為這個事務指定一個唯一的名字。這個名字不是必須的,除非你打算使用 FragmentManager.BackStackEntry 中的APIs來執行一些高階fragment操作。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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