文章目錄
- 一、Fragment的介紹
- 二、Fragment的生命周期
- 三、建立Fragment
- 四、在Activity中添加Fragment
-
- 1.通過布局檔案添加。
- 2.當Activity運作時動态添加Fragment(強大之處)
- 五、執行個體示範
一、Fragment的介紹
Fragment(碎片)是一種可以嵌入在活動中的UI片段,能夠讓程式更加合理和充分地利用大螢幕的空間,出現的初衷是為了适應大螢幕的平闆電腦,可以将其看成一個小型Activity,又稱作Activity片段。
使用Fragment可以把螢幕劃分成幾塊,然後進行分組,進行一個子產品化管理。Fragment不能夠單獨使用,需要嵌套在Activity中使用,其生命周期也受到宿主Activity的生命周期的影響
從官方的定義可以得到:
- Fragment依賴于Activity,不能獨立存在
- 一個Activity可以有多個Fragment
- 一個Fragment可以被多個Activity重用
- Fragment有自己的生命周期,并能接收輸入事件
- 可以在Activity運作時動态地添加或删除Fragment
Fragment的優勢:
- 子產品化(Modularity):我們不必把所有代碼全部寫在Activity中,而是把代碼寫在各自的Fragment中。
- 可重用(Reusability):多個Activity可以重用一個Fragment。
- 可适配(Adaptability):根據硬體的螢幕尺寸、螢幕方向,能夠友善地實作不同的布局,這樣使用者體驗更好。
二、Fragment的生命周期
從圖中可以看出,Fragment的生命周期與Activity的生命周期十分相似,但是比Activity多5種方法。
- onAttach():當Fragment和Activity建立關聯的時候調用
- onCreateView():為Fragment建立視圖(加載布局)時調用。
- onActivityCreated():當Activity的onCreate()方法傳回時調用
- onDestoryView():當該Fragment的視圖被移除時調用
- onDetach():當Fragment和Activity解除關聯時調用。
三、建立Fragment
在程式包名處單擊右鍵,選擇new–>Fragment->Fragment(Blank),即可進入Configure Component界面,在該界面指定Fragment名稱,以及Fragment對應的布局名稱。如圖所示;
四、在Activity中添加Fragment
将Fragment添加到Activity中,也就是實作Fragment與Activity的通信,有兩種方法:
1.通過布局檔案添加。
使用Fragment 時,隻需要将Fragment作為一個控件在Activity的布局檔案中引用即可。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:name="com.example.fragmentdemo.FragmentCourseList"
android:id="@+id/list_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:name="com.example.fragmentdemo.FragmentCourseDetail"
android:id="@+id/detail_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
2.當Activity運作時動态添加Fragment(強大之處)
Fragment的強大之處就在與可以動态的添加到Activity之中,這種方式更加靈活。
1.首先需要在activity_main中指定一個FrameLayout作為容器。
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:name="com.example.fragmentdemo.FragmentCourseList"
android:id="@+id/list_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/frame1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
2.當Activity運作時,在布局中動态的加入Fragment。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.建立Fragment執行個體
FragmentCourseDetail fragmentCourseDetail=new FragmentCourseDetail();
//2.擷取FragmentManager執行個體
FragmentManager fm=getSupportFragmentManager();
//3.擷取FragmentTransaction執行個體
FragmentTransaction transaction=fm.beginTransaction();
//4.添加一個Fragment
transaction.add(R.id.frame1,fragmentCourseDetail);
//4.更換一個Fragment
//transaction.replace(R.id.frame1,fragmentCourseDetail);
//5.送出事務
transaction.commit();
}
}
五、執行個體示範
程式界面分為左右兩部分,左側是導航欄(Activity),右側顯示具體的内容(Fragment)。當單擊左側的按鈕時,右側顯示相應欄目的内容,并且還能在兩個界面之間傳遞資料。
5.1 建立項目,并修改布局檔案,添加三個按鈕,用于顯示不同的Fragment。同時添加一個編輯框和送出按鈕用于傳遞資料。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="100dp">
<Button
android:text="院系介紹"
android:id="@+id/college"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="教師介紹"
android:id="@+id/teacher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="課程介紹"
android:id="@+id/course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="送出"
android:id="@+id/commit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<FrameLayout
android:id="@+id/frame1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
5.2 建立3個Fragment,并在第一個Fragment中添加一個文本框元件,用于接收左側欄目上的文本編輯框所傳遞的資料
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".FragmentCourseDetail">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="這是院系介紹" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
5.3在Main_Activity.java中采用動态替換fragment的方法,根據所選的欄目不同加載不同的Fragment。
package com.example.fragmentdemo;
...
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button college;
private Button teacher;
private Button course;
private EditText et;
private Button commit;
FragmentCourseDetail fg01;
FragmentCourseList fg02;
Fragment_03 fg03;
FragmentManager fm;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
replaceFragemnt(fg01);
}
private void initView() {
college = (Button) findViewById(R.id.college);
teacher = (Button) findViewById(R.id.teacher);
course = (Button) findViewById(R.id.course);
et = (EditText) findViewById(R.id.et);
commit = (Button) findViewById(R.id.commit);
fm = getSupportFragmentManager();
fg01=new FragmentCourseDetail();
fg02=new FragmentCourseList();
fg03=new Fragment_03();
college.setOnClickListener(this);
teacher.setOnClickListener(this);
course.setOnClickListener(this);
commit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.college:
replaceFragemnt(fg01);
break;
case R.id.teacher:
replaceFragemnt(fg02);
break;
case R.id.course:
replaceFragemnt(fg03);
break;
case R.id.commit:
String text=et.getText().toString();
fg01.setText(text);
break;
}
}
public void replaceFragemnt(Fragment fragment){
transaction=fm.beginTransaction();
transaction.replace(R.id.frame1,fragment);
transaction.commit();
}
}