先說明一下,本例子是運行在Android Studio下面的。同樣的代碼復制粘貼到Eclipse運行卻會報錯。具體原因我也沒有細查。知道的哥們,留言通知下唄。
Fragment,也就是碎片,本意是為了適配大屏幕的安卓設備而生的。但是出現后,很多安卓開發者都非常喜歡這個東西。這個東西很好用,但是也不是很容易用。下面我來來細細解說Android中的Fragment。
1、Fragment產生的緣由
運行Android的設備繁多,屏幕大小更是多種多樣。針對不同屏幕尺寸,通常情況下,開發者都是先針對手機開發一套源代碼,然后拷貝一份,修改布局以適應大屏幕設備,或平板,電視等。為了決解這樣的麻煩,Google推出了Fragment。你可以把Fragment當成Activity的一個界面的一個組成部分,甚至Activity的界面可以完全有不同的Fragment組成,Fragment擁有自己的生命周期和接收、處理用戶的事件,這樣就不必在Activity寫一堆控件的事件處理的代碼了。更為重要的是,你可以動態的添加、替換和移除某個Fragment。
2、Fragment的生命周期
Fragment必須是依存與Activity而存在的,因此Activity的生命周期會直接影響到Fragment的生命周期。官網這張圖很好的說明了兩者生命周期的關系:

可以看到Fragment比Activity多了幾個額外的生命周期回調方法:
onAttach(Activity)
當Fragment與Activity發生關聯時調用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
創建該Fragment的視圖
onActivityCreated(Bundle)
當Activity的onCreate方法返回時調用
onDestoryView()
與onCreateView想對應,當該Fragment的視圖被移除時調用
onDetach()
與onAttach相對應,當Fragment與Activity關聯被取消時調用
注意:除了onCreateView,其他的所有方法如果你重寫了,必須調用父類對於該方法的實現,
3、靜態的使用Fragment
這是使用Fragment最簡單的一種方式,把Fragment當成普通的控件,直接寫在Activity的布局文件中。步驟:
1、繼承Fragment,重寫onCreateView決定Fragemnt的布局
2、在Activity中聲明此Fragment,就當和普通的View一樣
下面展示一個例子(我使用2個Fragment作為Activity的布局,一個Fragment用於標題布局,一個Fragment用於內容布局):
TitleFragment的布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@android:color/darker_gray" android:layout_height="45dp"> <ImageButton android:id="@+id/im_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/star_on" /> <TextView android:textSize="25sp" android:textColor="@android:color/holo_red_dark" android:gravity="center" android:text="Fragment制作標題欄" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
ContentFragment布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:gravity="center" android:layout_height="match_parent"> <TextView android:gravity="center" android:textSize="25sp" android:text="Fragment當作主面板" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
MainActivity布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" > <fragment android:id="@+id/fg_title" android:layout_width="match_parent" android:layout_height="45dp" android:name="app.linfeng.com.myapplication.TitleFragment" /> <fragment android:layout_below="@id/fg_title" android:id="@+id/fg_content" android:layout_width="match_parent" android:layout_height="match_parent" android:name="app.linfeng.com.myapplication.ContentFragment" /> </RelativeLayout>
TitleFragment源代碼
package app.linfeng.com.myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.Toast; public class TitleFragment extends Fragment { private ImageButton im_button; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.title_fragment_layout,container,false); im_button = (ImageButton) view.findViewById(R.id.im_button); im_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"業務邏輯寫在Fragment上,Activity是不是很整潔了?",Toast.LENGTH_SHORT).show(); } }); return view; } }
ContentFragment源代碼
package app.linfeng.com.myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class ContentFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.content_fragment_layout,container,false); return view; } }
MainActivity源代碼
package app.linfeng.com.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; /** * 因為Android Studio新建Activity是自動繼承AppCompatActivity,所以我也沒有改成 * Activity,這個和本案例沒有關系哈。 */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE); //因為繼承的是AppCompatActivity,所以requestWindowFeature()失效了 getSupportActionBar().hide(); setContentView(R.layout.activity_main); } }
最后我們看看運行效果

靜態使用Fragment其實就是把Fragment當成普通的View一樣聲明在Activity的布局文件中,然后所有控件的事件處理等代碼都由各自的Fragment去處理,瞬間覺得Activity好干凈有木有~~代碼的可讀性、復用性以及可維護性是不是瞬間提升了
