Fragments是Android 3.0 (API level 11)才引入的.但是它却又是向下兼容的.可以支持老的Android版本.

只不过需要导入jar包支持(在这个目录下:android-sdk-windows\extras\android\support\v4\android-support-v4.jar),

主要用于实现以下这种UI布局

想要实现这样一个activity里面有多个复杂的View布局, 按照以前的惯用写法可以使用viewgroup 或者自定义一些布局来实现

而Fragments就恰恰满足了我们这一需求,他相比我们原来实现的方法,功能更强大,

简单说来,我个人觉得可以把Fragments看成是一个view,他比view强大的地方是他是有生命周期的 并且这个生命周期会随着他附着的那个activity的改变而改变.

onCreate()The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

onCreateView()The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return aViewfrom this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

onPause()The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

这是他的三个主要的方法,或者说生命周期.当他的父activity onPause();的时候,他也会调用onPause();同样他的父activityonResume();他调用onResume();

这样就很灵活,比如有大量bitmap的view,就可以在onPause();释放掉无用的资源,再在onResume()的时候加载.

这些Fragments是由FragmentManager这样一个类似栈的容器管理的,可以通过findFragmentById()或者findFragmentByTag()找到添加到页面里面的Fragments.

下面看个小小的DEMO:

首先我们要写一个继承Fragment的类,你可以把他看成你的UI界面里的某一个模块

public class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println(" onCreateView ");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println(" onCreate ");
super.onCreate(savedInstanceState);
}

@Override
public void onPause() {
System.out.println(" onPause ");
super.onPause();
}

@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println(" onResume ");
}

}

根据打印输出,你会发现只有第一次启动才会调用onCreateView;而这里面就是你这个UI模块的布局

接着是你要展示的activity:

public class MyFragmentActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ExampleFragment exampleFragment = new ExampleFragment();
// FragmentManager fragmentManager = getSupportFragmentManager();
// FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.add(R.id.viewer, exampleFragment);
// fragmentTransaction.commit();
}

}

然后activity的布局也非常简单

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:orientation
="horizontal" >

<fragment
android:id="@+id/list"
android:name
="com.fragment.demo.ExampleFragment"
android:layout_width
="0dp"
android:layout_height
="match_parent"
android:layout_weight
="1" />

<fragment
android:id="@+id/viewer"
android:name
="com.fragment.demo.ExampleFragment"
android:layout_width
="0dp"
android:layout_height
="match_parent"
android:layout_weight
="2" />

</LinearLayout>

这样就已经搞定,生成了2个fragment, 这算是静态的添加吧

如果是动态的添加那就需要用到上面注释的代码:

        ExampleFragment exampleFragment = new ExampleFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.viewer, exampleFragment);
fragmentTransaction.commit();

差不多初识就是这样,希望有人能补充,下个项目有机会一定要尝试用用

更多相关文章

  1. Android(安卓)--- Activity生命周期
  2. Android(安卓)Wifi模块分析(三)
  3. Android中dispatchDraw分析
  4. Android四大基本组件介绍与生命周期
  5. Android(安卓)Service AIDL
  6. Android调用天气预报的WebService简单例子
  7. android打电话发短信
  8. android 拨打紧急号码,通话时开启免提功能实现
  9. Android常用控件

随机推荐

  1. 学习android的博客
  2. Android - 开发常用工具类Utils
  3. Android Studio中的mipmap和drawable
  4. Android 音频降噪 webrtc 去回声
  5. android LayoutInflater.inflate()的参数
  6. listView当中有嵌套了有onClickListener
  7. setAnimationStyle实现的popwindow显示消
  8. 防止Android点击按钮过快造成多次事件实
  9. Android实现授权访问网页的方法
  10. 【Android】删除已知路径的文件或文件夹