在Android中,Fragment是比价常用的一个类,注意,Fragment是个类,只实现了两个接口:
1.ComponentCallbacks
2.OnCreateContextMenuListener
第一个接口:官方文档上解释,Called by the system when the device configuration changes while your
component is running。什么意思呢,当你的应用正在运行,这个时候你的设备配置改变了,系统会自动调用这个接口。
这个接口里面就两个回调方法:
A void onConfigurationChanged(Configuration newConfig);--->当配置改变的时候调用
B void onLowMemory(); --->当内存低的时候调用此方法。
第二个接口:OnCreateContextMenuListener,官方文档解释,Interface definition for a callback to be invoked when the context menu for this view is being built. 当前view的上下文菜单创建的时候调用。
这个接口里面就一个回调:
void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
当创建上下文菜单的时候调用。
Fragment第一次出现时在谷歌的平板上,因为平板设备比较大,如果仅仅显示简单的布局有点浪费,为了充分利用那么大的屏幕,Fragment出现了。Fragment是碎片的意思,一听就知道这个类是比较小的,一般比较小的东西消耗资源就比价小,对于手机这种小型设备,资源是非常宝贵的,所以先在Fragment在手机中的应用也很广泛。Fragment在一定程度上可以替代Activity进行页面的显示。我就把Fragment理解为一个轻量级的Activity,基本上能够满足所有需要在Activity上进行的东西,比如显示view,请求网络,页面跳转等。
下面说下Fragment的生命周期:
Fragment是依赖于Activity存在的,有点像盆栽,首先你的花盆得有,然后才能种盆栽么。
所以Fragment得生命周期有点多:
官方文档如下(如果想看中文的可以百度下,但是建议看英文的,因为很多技术文档还是英文的,掌握一门外语也不错哈哈):
The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
- onAttach(Activity) called once the fragment is associated with its activity.
- onCreate(Bundle) called to do initial creation of the fragment.
- onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
- onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
- onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
- onStart() makes the fragment visible to the user (based on its containing activity being started).
- onResume() makes the fragment interacting with the user (based on its containing activity being resumed).
As a fragment is no longer being used, it goes through a reverse series of callbacks:
- onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
- onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
- onDestroyView() allows the fragment to clean up resources associated with its View.
- onDestroy() called to do final cleanup of the fragment's state.
- onDetach() called immediately prior to the fragment no longer being associated with its activity.
下面,我来说下Fragment怎么使用:
1.在布局文件中使用,这个时候需要name属性和fragment标签。
首先建立Fragment类:
public class DemoFragment extends Fragment{
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
TextView textView=new TextView(getActivity());
textView.setText("静态使用Fragment");
return textView;
}
}
接着布局文件:
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:textSize="30dp"
android:gravity="center"
android:background="#66FF0000"
android:text="Fragment的静态使用" />
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="com.example.fragmentdemo.DemoFragment"
/>
</LinearLayout>
直接运行,Fragment就被加载到应用中。
需要注意的是,使用这种方式,Fragment就相当于在布局文件中的view,里面的控件可以直接通过findViewById()找到。比较方便,但是如果有切换页面的时候就显得不那么灵活了。