編輯:關於 Android 開發
Fragment 的核心用途是在一個 Activity 內部切換界面——切換時只替換 Fragment 容器裡的內容,Activity 本身保持不變。它的生命週期方法與 Activity 基本一致,可以粗略理解為「一個可以被隨時替換的 Activity」。
第一步,定義布局文件作為 Fragment 的顯示內容:
第二步,編寫 Fragment 類,在 onCreateView() 中加載布局並返回:
public class fragment1 extends Fragment {
@Nullable
@Override
// 返回的 view 對象會作為 fragment 的內容顯示在屏幕上
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, null);
return v;
}
}
第三步,在 Activity 中通過 FragmentManager 開啟事務並替換:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click01(View v) {
fragment fg1 = new fragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.f1, fg1);
ft.commit();
}
public void click02(View v) {
fragment1 fg2 = new fragment1();
getFragmentManager().beginTransaction().replace(R.id.f1, fg2).commit();
}
public void click03(View v) {
fragment2 fg3 = new fragment2();
getFragmentManager().beginTransaction().replace(R.id.f1, fg3).commit();
}
}
Activity 的布局中需要預留一個容器,這裡用 FrameLayout:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="fragment01" android:onClick="click01"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="fragment02" android:onClick="click02"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="fragment03" android:onClick="click03"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/f1">
</FrameLayout>
</LinearLayout>
每個 Fragment 各自的布局很簡單,例如 fragment1 就是一個紅色背景加一行文字:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="紅色"/>
</LinearLayout>
運行效果:點擊左側三個按鈕,右側區域顯示對應的 Fragment。
Fragment 切換時,舊的 Fragment 對象會被銷毀,新的 Fragment 對象會被創建。這意味著:
replace() 都是一次完整的創建—銷毀循環,Fragment 中未保存的臨時狀態會丟失,需要時應在 onSaveInstanceState() 中自行持久化。hide()/show() 而不是 replace(),代價是內存佔用更高。commit() 前用 setCustomAnimations() 或 setTransition() 添加動畫。support-v4 包中提供了 Fragment 相關 API,因此 Fragment 可以在低版本系統上正常使用。使用時要注意兩套 API 不能混用:
android.app.Fragment(API 11+)搭配 getFragmentManager();android.support.v4.app.Fragment 搭配 getSupportFragmentManager()。目前新建項目通常直接使用 AndroidX 的 androidx.fragment.app.Fragment,配合 FragmentContainerView 或 FragmentManager.commit() 完成切換,功能更完整,也更容易與 ViewModel、Navigation 組件協同。
仿淘寶商品屬性標籤頁:自定義 ViewGroup 自動換行
仿淘寶商品屬性標籤頁:自定義 ViewGroup 自動換行電商商品頁常需要動態顯示顏色、尺碼、款式等屬性,且每個屬性下的標籤數量不固定。若使用普通 LinearLayou
FloatingActionButton 隨列表滾動滑入滑出
FloatingActionButton 隨列表滾動滑入滑出FloatingActionButton 是常見的懸浮操作按鈕。makovkastar 的開源實作可讓按鈕依附
Android Activity 生命週期詳解:回調順序、螢幕旋轉與狀態保存
Android Activity 生命週期詳解:回調順序、螢幕旋轉與狀態保存 Activity 的生命週期機制類似 Java EE 中的 Servlet 容器模型。開發者
Android 開發學習筆記:Camera 初體驗,拍照、裁剪與相冊選圖
從 Camera 說起 顧名思義,Camera 就是拍照與錄影的功能。像微信裡想拍張照片傳給朋友,背後走的就是這條路。但要注意的是,多數 App 並不會直接呼叫 Came