Fragment的介绍
在我们Android开发当中,Fragment是我们常用的组件之一,Fragment是一种可以嵌入到活动当中的UI片段,它和Activity非常的相似,所以学习起来相对容易些。Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应其他尺寸。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。
Fragment的生命周期
Fragment必须是依存Activity而存在的,和Activity一样,Fragment也有自己的生命周期,并且生命周期也是非常的相似。下面一图很好的说明了两者生命周期的关系
活动中有的回调方法,Fragment中几乎都有,不过Fragment还提供了一些附加的回调方法,下面说明一下这几个回调方法。
onAttach():当Fragment和Activity建立关联时候调用
onCreateView():Fragment创建视图时调用
onActivityCreated():确保与Fragment相关联的Activity一定已经创建完毕的时候调用
onDestroyView():当该Fragment的视图被移除的时候调用
onDetach():当Fragment和Activity解除关联的时候调用
Fragment的使用
下面写一个简单使用Fragment的例子,在一个Activity中添加两个Fragment,并让这两个碎片平分Activity空间
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/above_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<fragment
android:id="@+id/below_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
再建一个上面的Fragment布局,above_fragment.xml,代码如下:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
</LinearLayout>
再建一个下面的Fragment布局,below_fragment.xml,代码如下:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#3F51B5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right fragment"
android:textColor="#FFFFFF"/>
</LinearLayout>
左侧Fragment布局只有一个按钮,右边Fragment布局只有一个文本,背景用颜色区分。接着新建AboveFragment 类,继承Fragment。
public class AboveFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.above_fragment, container, false);
return view;
}
}
这里重新了Fragment的onCreateView()方法然后再通过LayoutInflater 的inflate()方法将布局加载进来,再用同样的方法新建一个BelowFragment 。
public class BelowFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.below_fragment, container, false);
return view;
}
}
接着修改activity_main.xml中的代码,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/above_fragment"
android:name="com.mountain.mytest.AboveFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<fragment
android:id="@+id/below_fragment"
android:name="com.mountain.mytest.BelowFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
在fragment标签添加属性name来显示指定添加的Fragment类名。这样简单的例子就完成了。效果如下
上面的例子是静态加载Fragment,下面使用动态加载Fragment。我们还是在原来的基础上进行修改,再创建一个Fragment布局,another_below_fragment.xml,代码如下:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#4CAF50"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right fragment2"
android:textColor="#FFFFFF"/>
</LinearLayout>
这个布局文件和below_fragment.xml基本相同,只是修改了背景色,还有文字,再新建AnotherBelowFragment,代码如下:
public class AnotherBelowFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_below_fragment, container, false);
return view;
}
}
接着修改activity_main.xml,代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/above_fragment"
android:name="com.mountain.mytest.AboveFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/below_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
下面我们将在代码想FrameLayout添加内容,从而实现动态添加Fragment,MainActivity代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
replaceFragment(new BelowFragment());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new AnotherBelowFragment());
}
});
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.below_layout, fragment);
transaction.commit();
}
}
在MainActivity中写了一个replaceFragment方法,这个方法的作用是动态添加Fragment,主要分为4步:
(1)获取FragementManager,在Activity在可以直接调用getSupportFragmentManager()方法得到。
(2)开启一个事务没通过FragementManager的beginTransaction()方法开启。
(3)向容器添加或替换Fragment,一般使用replace()方法实现,需要传入容器的id和待添加的Fragment实例
(4)提交事务,调用commit()方法来完成。
这样就完成了动态添加Fragment的功能。效果如下:
刚进MainActivity时和上个例子一样
点击按钮后,下面的Fragment替换