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).

这是他的三个主要的方法,或者说生命周期.当他的父activityonPause();的时候,他也会调用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();

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

android

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

posted @2011-12-22 15:58Mr_Su 阅读(505) |评论 (0)编辑|

Android Widget添加自定义控件

posted @2011-12-22 14:59Mr_Su 阅读(55) |评论 (0)编辑|

关于地图拍照上传项目的一些总结

posted @2011-12-22 14:59Mr_Su 阅读(96) |评论 (0)编辑|

更多相关文章

  1. android各层调用关系,架构流程
  2. WebView Android(安卓)调用js且须要获取返回结果
  3. Android端JQueryMobile使用教程(二)
  4. Android(安卓)四大组件(Activity、Service、BroadCastReceiver、
  5. 第99章、Android调用Javascript(从零开始学Android)
  6. android service 学习(上)
  7. AIR Native Extension的使用(Android)二 : Flex mobile项目中使
  8. Android中WebView使用规范
  9. android中listview的setAdapter()和getAdapter()

随机推荐

  1. Android之Intent深入
  2. android下的蓝牙A2DP
  3. [闲话杂谈] Android(安卓)跟 Linux 是有
  4. 转载:谁说程序员不浪漫---Android爱心表白
  5. Android平板电脑远程控制PC机教程
  6. Android音频编辑之音频合成功能
  7. 浅谈android的mount命令
  8. [置顶] 搜集整理的一些博客导航
  9. Android(安卓)- 小功能 - Android中dp和p
  10. Android工程引用另外一个工程的正确/错误