編輯:關於 Android 開發
自Android 3.0起,系統引入了Fragment,主要目的是為了在大螢幕裝置上實現更動態、更靈活的UI設計。Fragment應當被視為一個模組化、可重複使用的組件——它定義了自己的佈局,並透過生命週期回呼方法定義了自己的行為,因此可以被嵌入到多個Activity中使用。
關於Fragment,你需要掌握以下要點:
(1)Fragment可以作為Activity介面的一部分呈現;
(2)同一個Activity中可同時存在多個Fragment,而一個Fragment也能被多個Activity共用;
(3)在Activity運行期間,可以動態地新增、移除或替換Fragment;
(4)Fragment能回應自己的輸入事件,擁有獨立的生命週期,但其生命週期受宿主Activity影響;
(5)當Fragment首次繪製介面時,系統會呼叫onCreateView()方法,此方法回傳一個View(若不顯示UI則回傳null)。
Fragment有兩種加載方式:靜態加載與動態加載。本文聚焦於靜態加載。
本文將以一個完整的APP為例講解Fragment的知識,大致佈局如下:
首先在values中新增color.xml:
<resources>
<color name="gray">#88000000</color>
<color name="white">#ffffff</color>
</resources>
接著在drawable中新增radio_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@color/gray" android:state_checked="true"></item>
<item android:drawable="@color/white" android:state_pressed="true"></item>
<item android:drawable="@color/white"></item>
</selector>
主佈局main.xml:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal">
<RadioButton
android:id="@+id/first"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/radio_pressed"
android:button="@null"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center_horizontal"
android:text="靜態加載" />
<!-- 其餘RadioButton省略,分別為動態加載、生命週期、傳值通信 -->
</RadioGroup>
</RelativeLayout>
MainActivity加載main佈局:
public class MainActivity extends Activity implements OnCheckedChangeListener {
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
group = (RadioGroup) findViewById(R.id.radiogroup);
group.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.first: // 演示靜態加載
break;
case R.id.second: // 演示動態加載
break;
case R.id.thrid: // 演示生命週期
break;
case R.id.fourth: // 演示傳值通信
break;
}
}
}
靜態加載的關鍵,是在Activity的layout檔案中直接聲明Fragment。特別注意:<fragment> 標籤中的 android:name 屬性指定了要在佈局中實例化的Fragment類。標識Fragment的方法有兩種:A. 用 android:id 屬性提供唯一ID;B. 用 android:tag 屬性提供唯一字串。
先新增fragment.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的Fragment" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" />
</LinearLayout>
新增MyFragment類並載入fragment佈局:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// 將layout佈局檔轉換成View物件
// inflater.inflate(resource, root, attachToRoot)
// attachToRoot為false時,不返回父ViewGroup
View view = inflater.inflate(R.layout.fragment, container, false);
TextView text = (TextView) view.findViewById(R.id.text);
Button button = (Button) view.findViewById(R.id.button);
text.setText("靜態加載Fragment");
button.setText("獲取內容");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 在這裡處理點擊事件
}
});
return view;
}
}
接著新增jigntai.xml,透過fragment標籤嵌入Fragment:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- android:id:靜態加載必須指定一個ID -->
<!-- android:name:完整套件名稱 -->
<fragment
android:id="@+id/fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.fragment.MyFragment" />
</LinearLayout>
新增JingTaiActivity類別:
public class JingTaiActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jingtai);
Button button = (Button) findViewById(R.id.button);
tv = (TextView) findViewById(R.id.text);
button.setText("改變");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("TextView改變了");
}
});
}
}
在MainActivity中,為「靜態加載」這一項加入跳轉邏輯:
case R.id.first: // 演示靜態加載
Intent jingtaiIntent = new Intent(MainActivity.this, JingTaiActivity.class);
startActivity(jingtaiIntent);
break;
整體流程是這樣的:MainActivity跳轉到JingTaiActivity,後者載入了包含一個 <fragment> 的jigntai.xml,而該fragment的android:name指向com.example.fragment.MyFragment,這個MyFragment又有自己的text與button佈局。回到JingTaiActivity後,我們可以直接透過findViewById找到MyFragment佈局中的text與button。
由此可以得出結論:當一個佈局檔透過靜態加載方式將Fragment載入Activity後,Fragment內部的佈局對Activity而言也是共享的。
Android 記憶體回收機制解析:Framework 與 Linux 核心的協同
Android 的執行環境Android 基於 Linux 核心,但為了適應行動裝置,對應用執行與資源管理做了許多調整。每個應用通常對應一個 Dalvik VM 實例與一
Android Studio 中使用 OpenCV 的三種方式詳解
告別 OpenCV Manager 的三種整合方案 OpenCV 是強大的電腦視覺庫,在 Android 上也有廣泛應用。最早接觸它時還是 Eclipse 時代,那時網上
NiftyNotification 生動通知效果
NiftyNotification 生動通知效果NiftyNotification 是一個可替代原生 Toast 的開源通知元件,提供多種進場動畫,例如 scale、th
Android Studio 生成簽名 APK 完整流程
Android Studio 生成簽名 APK 完整流程在 Android Studio 中打包正式安裝檔,需要透過 Generate Signed APK 建立或選擇簽