標籤:
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好乾淨有木有~~代碼的可讀性、複用性以及可維護性是不是瞬間提升了
Android Fragment用法詳解(1)--靜態使用Fragment