show code block

2016年6月16日 星期四

Fragment的使用(一) ─ 在activity內放置Fragment

Fragment的使用()
activity內放置Fragment


前言:

(更新聲明2017/03/08)
做個小聲明:在這邊建議太過複雜的畫面不要使用Fragment。
這聲明是流著淚打出來的,Fragment是個超級大坑,以後妳絕對會遇到各式各樣不可預期的爛帳。我敢說Fragment的絕對是Android最爛的設計知之一。

1、如果你拿Fragment作為你的分頁使用,最常見的問題就是OOM,分頁之間的切換導致Fragment不斷疊加。
參照:http://stackoverflow.com/questions/28483600/android-fragments-on-backstack-taking-up-too-much-memory

2、再來就是複雜、難以控制的的生命週期
參照:https://asce1885.gitbooks.io/android-rd-senior-advanced/content/wo_wei_shi_yao_zhu_zhang_fan_dui_shi_yong_android_fragment.html

不是我在這黑Fragment,上面聯結有一小段說道:

When Fragments were introduced, they sounded like the best idea of all times. Being able to display more than one activity at the same time, kinda. That was the selling point.
當Fragment剛被發明,當你的App想要展示多個Activity時,Fragment被認為是最好的使用方式。
So the whole world slowly started using Fragments. It was the new kid on the block. Everybody was using Fragments. If you were not using Fragments, chances were that "you were doing it wrong".
Fragment當時是一個新的功能,全世界開始慢慢使用它的當下,你只要沒有使用Fragment,就會有人認為你的作法不夠潮。
A few years and apps later, the trend is (thankfully) reverting back to more activity, less fragment. This is enforced by the new APIs (The ability to transition between activities without the user really noticing, as seen in the Transition APIs and such).
幾年過後,因為API的改動,人們開始傾向於改回使用Activity多於Fragment。
So, in summary: I hate fragments. I believe it's one of the worst Android implementations of all time, that only gained popularity because of the lack of Transition Framework (as it exists today) between activities. The lifecycle of a Fragment is, if anything, a ball of random callbacks that are never guaranteed to be called when you expect them.

總結:Fragment是Android 有史以來最爛的成就,Fragment會變得如此火熱的原因是因為他在切換畫面時的流暢度是Activity無法比擬的。

所以我的結論是:
複雜的頁面不要使用Fragment!!!
主要的用途在ViewPager之間的轉換就好了。





為什麼要在Activity加入Fragment呢?
為了要做到兩件事。

一、主要目的是要讓App得執行畫面,可以隨著裝置的螢幕大小自動調整。(本日實作)


二、可以在一個Activity內作內容的切換。






這裡我們先講最簡單的方式,完全不用動用到Java程式碼,可以直接在Layout佈局中完成的。




STEP 1
開創一個Fragment






















在創建的過程中,紅框處請勿勾選,用不到。





STEP2
LAYOUT拉出fragment的布局



fragment_blank中的布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.nikeru8.fragmentforone.BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我是Fragment,大家好!"
        android:textSize="50dp"/>

</FrameLayout>











activity_main的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.nikeru8.fragmentforone.MainActivity">


    <fragment android:name="com.example.nikeru8.fragmentforone.BlankFragment"
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_blank" />

</LinearLayout>

 -name
指定妳要顯示的fragment位置。
-id
必定要加上id這個選項。如果沒有在fragment佈局中放入id,app開啟時就會崩潰。
-tools:layout
此功能只是一個工具,讓妳能在Preview中顯示fragment的內容。










妳看到此畫面就是完全體了,在Activity內設置了一層Fragment

但因為是用(-name:)這個方法寫死在Layout佈局中,無法做切換。

下一章節將提到如何在Activity中切換Fragment





以上兩步驟輕鬆搞定Fragment。






 可能會遇到的問題

在layout的布局中大多數的開頭都會是大寫,但在布局中 fragment 的開頭字母 「f」 必定是小寫。









Fragment全集
 

3 則留言:

  1. 請問怎樣才能算是複雜呢?

    我目前是打算使用ViewPager + Fragment

    每個Fragment中自己去抓取資料

    回覆刪除
    回覆
    1. 抱歉回覆慢了,複雜的定義每個人不同。
      如果你有把握能把fragment玩弄於股掌間,當然你想怎麼用都行。
      個人建議除了顯示固定的畫面以外都不用使用就是了。
      in your case
      要看你抓取怎樣類型的資料,多寡與否

      刪除
    2. 如果我是你,我會使用TabLayout +ViewPager來做。

      刪除

協程(coroutine) - 協程為什麼要學它?

 Coroutine 協程 再強調一次 協程就是由kotlin官方所提供的線程api //Thread Thread { }.start() //Executor val execu...