編輯:關於 Android 程式設計
先把位置佔著,翻譯待補。以下暫留原文,日後再整理成中文。
When designing your application to support a wide range of screen sizes, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space.
For example, on a handset device it might be appropriate to display just one fragment at a time for a single-pane user interface. Conversely, you may want to set fragments side-by-side on a tablet which has a wider screen size to display more information to the user.

Figure 1. Two fragments, displayed in different configurations for the same activity on different screen sizes. On a large screen, both fragments fit side by side, but on a handset device, only one fragment fits at a time so the fragments must replace each other as the user navigates.
The FragmentManager class provides methods that allow you to add, remove, and replace fragments to an activity at runtime in order to create a dynamic experience.
Rather than defining the fragments for an activity in the layout file—as shown in the previous lesson with the <fragment> element—you can add a fragment to the activity during the activity runtime. This is necessary if you plan to change fragments during the life of the activity.
To perform a transaction such as add or remove a fragment, you must use the FragmentManager to create a FragmentTransaction, which provides APIs to add, remove, replace, and perform other fragment transactions.
If your activity allows the fragments to be removed and replaced, you should add the initial fragment(s) to the activity during the activity's onCreate() method.
An important rule when dealing with fragments—especially those that you add at runtime—is that the fragment must have a container View in the layout in which the fragment's layout will reside.
The following layout is an alternative to the layout shown in the previous lesson that shows only one fragment at a time. In order to replace one fragment with another, the activity's layout includes an empty FrameLayout that acts as the fragment container.
Notice that the filename is the same as the layout file in the previous lesson, but the layout directory does not have the large qualifier, so this layout is used when the device screen is smaller than large because the screen does not fit both fragments at the same time.
res/layout/news_articles.xml:
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Inside your activity, call getSupportFragmentManager() to get a FragmentManager using the Support Library APIs. Then call beginTransaction() to create a FragmentTransaction and call add() to add a fragment.
You can perform multiple fragment transaction for the activity using the same FragmentTransaction. When you're ready to make the changes, you must call commit().
For example, here's how to add a fragment to the previous layout:
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// 確認目前使用的是含 fragment_container 的版面版本
if (findViewById(R.id.fragment_container) != null) {
// 若是從先前狀態被還原回來,就不要重複加入,否則會出現重疊
if (savedInstanceState != null) {
return;
}
HeadlinesFragment firstFragment = new HeadlinesFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment)
.commit();
}
}
}
Because the fragment has been added to the FrameLayout container at runtime—instead of defining it in the activity's layout with a <fragment> element—the activity can remove the fragment and replace it with a different one.
The procedure to replace a fragment is similar to adding one, but requires the replace() method instead of add().
Keep in mind that when you perform fragment transactions, such as replace or remove one, it's often appropriate to allow the user to navigate backward and "undo" the change. To allow the user to navigate backward through the fragment transactions, you must call addToBackStack() before you commit the FragmentTransaction.
Note: When you remove or replace a fragment and add the transaction to the back stack, the fragment that is removed is stopped (not destroyed). If the user navigates back to restore the fragment, it restarts. If you do not add the transaction to the back stack, then the fragment is destroyed when removed or replaced.
Example of replacing one fragment with another:
ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // 用這個 Fragment 取代容器中的內容,並加入回退堆疊 transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); transaction.commit();
The addToBackStack() method takes an optional string parameter that specifies a unique name for the transaction. The name isn't needed unless you plan to perform advanced fragment operations using the FragmentManager.BackStackEntry APIs.
雖然正文尚未翻完,這幾點可以先記下來:
同一個 Fragment 可以在不同版面配置中重複使用。手機上單欄一次顯示一個,平板雙欄並排顯示,靠的是不同的 layout 目錄限定符,而非不同的 Fragment 程式碼。
執行期加入的 Fragment 必須有容器。版面裡要預留一個空的 FrameLayout 當佔位,否則無處安放。
add / remove / replace 都需要 FragmentTransaction,且要 commit() 才生效。
addToBackStack() 決定使用者能否用返回鍵「復原」。不加入堆疊,被移除的 Fragment 就直接被銷毀;加入堆疊,它只是被 stop,返回時會重新啟動。
savedInstanceState 判斷不可省。螢幕旋轉等狀態還原情境下,若無條件重新 add,畫面上會出現兩個重疊的 Fragment。
從需求到上線:天氣預報 App 開發流程完整解析
許多新手在拿到需求後,常會卡在「不知道從哪裡開始」。本文以一個簡單的天氣預報 App 為例,說明從需求梳理、原型設計、架構選擇到開發上線的完整思路。 宏觀流程:一個專案如
Android Bound Service(一):以 Extending Binder 實作進程內綁定
前言 重新把 Bound Service 學一遍,是因為當年實在沒好好學。那時候總覺得「能動、能交差就好」,這種心態嚴重拖慢了技術成長,這個部落格裡有不少都是那段時期的複
React Native 實現 ListView 分組與懸浮吸頂效果
RN 裡做分組列表,思路和 Android 的 ExpandableListView 頗為相似,只是表現形式上更接近 iOS 的 UITableView:分組標題整條通
手機卡未實名要被停機?三種辦理途徑整理
手機卡未實名可能面臨停機,那實名登記該怎麼辦?隨著手機卡實名制於9月1日大規模推行,不少用戶開始著急。下面小編把辦理方式整理清楚。 整體來看,辦理實名可分為線下與線上兩大