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

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

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

Fragments的初识---不知道Fragments的不是合格的android开发

想要实现这样一个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实现计算器布局(四种布局方式)之LinearLayout线性布局
  2. Android常用布局有5种
  3. android中的布局简要介绍
  4. Android 短信模块分析(二) MMS中四大组件核心功能详解
  5. Activity的布局
  6. 安卓布局被软键盘顶上去的解决方案
  7. 布局动画 属性初始 android:animateLayoutChanges

随机推荐

  1. Android自定义视图三:给自定义视图添加“
  2. Android_TextView属性XML详解
  3. android mms流播放器
  4. android系统体系结构
  5. Android中的异步消息处理机制Hander
  6. OMS:拥有Android血统的智能操作系统王
  7. Android的消息机制,用Android线程间通信的
  8. Android自定义视图四:定制onMeasure强制显
  9. Android上的Back键事件捕获
  10. 自定义VIEW③Canvas