iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 9
0
Software Development

英國研究顯示,連續30天用Kotlin開發Android將有益於身心健康系列 第 9

Android Kotlin 實作 Day 8:BottomNavigation(上)(Fragment 生命週期與載入)

使用語言

  • Kotlin

使用元件

  • ImageView
  • TextView
  • menu
  • BottomNavigationView
  • Fragment

Layout 配置

  1. BottomNavigationView

    1. 自定義一個 menu 的 xml 檔,作為 BottomNavigationView 的內容

      關於自定義 menu 可參考 Day 1 內容

    2. 在 Layout 中加入 BottomNavigationView,並加入自定義的 menu

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFCCCC"  //背景顏色
        app:itemIconTint="#FFFFFF"    //Icon 顏色
        app:itemTextColor="#FFFFFF"   //文字顏色
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation"/> //加入自定義 menu 
    
  2. 導入 Fragment

    1. File > New > Fragment > Fragment (Blank)

    2. 命名 Fragment 及 Layout 名稱
      (紅框中為進階選項,不用勾選)

    3. 建立完後將會看到建立好的 Frgament 及 Layout

Method


Fragment

Fragment 為依附在 Activity 上的類別,有自己的生命週期,一個 Activity 可載入多個 Fragment,並做切換或分割 Activity 畫面。

  • Fragment 生命週期


    (圖片來源:Android 官方文件)

    • onAttach():Fragment 被添加到 Activity 時調用

    • onCreat():Fragment 被建立時調用

    • onCreatView():每次建立 Fragment 的 View 時調用,會回傳 View。

      • 當已建立過但放在 back stack 中的 Fragment 從 back stack 中被推出時,會從此方法開始,並重建 View。

        關於 back stack 會在後面 FragmentManager 中說明

      • 當 Fragment 透過 FragmentTransaction 的 attach 方法取回時會從此方法開始,並重建 View。

    • onActivityCreated():Fragment 所在的 Activity 被建立完成後調用

    • onStart():Fragment 被啟動時調用

    • onResume():onStart() 方法執行完後會自動執行此方法

      此方法完成後 Fragment 便會出現在畫面中,使用者開始可以與 Fragment 互動。

    • onPause():Fragment 進入暫停或放進 back stack 時被調用

    • onStop():onPause() 方法執行完後自動執行此方法

    • onDestroyView():Fragment 的 View 被清除時調用

      • Fragment 被放進 back stack 時會進行到此方法後停止

      • Fragment 透過 FragmentTransaction 的 detach 方法被分離時會執行到此方法後停止

    • onDestroy():Fragment 被清除時調用

    • onDetach():當 Fragment 與 Activity 完全斷開關係時調用

      • 透過 FragmentTransaction 的 remove 方法移除 Fragment 時會執行到此方法後停止

Fragment 有靜態載入和動態載入兩種 載入方式

  • 靜態載入

    在 Activity 的 Layout 中插入 Fragment
    Fragment 會在 Activity 建立好後就建立並被呼叫

    # 靜態載入的 Fragment 將無法被移除

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/LinearLayout1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context=".MainActivity" >
    
      <fragment
          android:id="@+id/frag"
          class = "com.example.fish.day8_bottomnavigationfragmentintent.Fragment_0"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
    
    </LinearLayout>
    
    • Fragment 一定要加上識別用的 id 或 tag(至少設置一種)

      • android:id="":Fragment 的識別 id

      • android:tag="":Fragment 的識別 tag

    • 指定寫好的 Fragment Class,有下列兩種方式

      • class="packageName.className"

      • android:name="packageName.className"

  • 動態載入

    在 Activity 中載入 Fragment
    可在 Activity 運作時,需要的時候再進行載入

    val fragment_0 = Fragment_0()
    val manager = supportFragmentManager
    val transaction = manager.beginTransaction()
    transaction.add(R.id.forFragment, fragment_0, tag).commit()
    

    關於 FragmentManager 及 FragmentTransaction 將在下篇介紹

查看詳細 Code > GitHub

tags: Android Kotlin Fragment BottomNavigationView

接下來關於 Fragment Class 及 FragmentManager 請參考 下篇 >>


上一篇
Android Kotlin 實作 Day 7_MyLocation(下)(GoogleMap相機、標記+SnackBar+定位服務)
下一篇
Android Kotlin 實作 Day 8:BottomNavigation(中)(Fragment Class 與 FragmentManager)
系列文
英國研究顯示,連續30天用Kotlin開發Android將有益於身心健康30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言