文章目录
- 一、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();
}
}