当前位置: 移动技术网 > IT编程>移动开发>Android > Android应用开发中使用Fragment的入门学习教程

Android应用开发中使用Fragment的入门学习教程

2019年07月24日  | 移动技术网IT编程  | 我要评论

小修士的奋斗,photoshop在线,妈妈吉利小叮当

  fragment是android honeycomb 3.0开始新增的概念,fragment名为碎片不过却和activity十分相似,下面介绍下android fragment的作用和用法。fragment用来描述一些行为或一部分用户界面在一个activity中,你可以合并多个fragment在一个单独的activity中建立多个ui面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块 ,fragment有自己的生命周期,接收自己的输入事件,你可以添加或移除从运行中的activity.

       一个fragment必须总是嵌入在一个activity中,同时fragment的生命周期受activity而影响,举个例子吧,当activity 暂停,那么所有在这个activity的fragments将被destroy释放。然而当一个activity在运行比如resume时,你可以单独的操控每个fragment,比如添加或删除。

       fragment作为android 3.0的新特性,有些功能还是比较强大的,比如 合并两个activity,如图

2016225102811270.gif (555×174)

 如上图所示,用activity a 表示文章标题列表,activityb表示文章具体内容。我们可以看到两个activity通过两个fragment合并到一个activity的布局方式,对于平板等大屏幕设备来说有着不错的展示面板。不过因为fragment和activity的生命周期都比较复杂,下图表示的fragments的生命周期:

2016225102831717.gif (319×768)

activity、fragment分别对比下:

2016225102909568.gif (403×691)

两个的生命周期很类似,也息息相关。
 
 
创建一个fragment你必须创建一个fragment的子类或存在的子类,比如类似下面的代码

public static class androidfragment extends fragment {
 @override
 public view oncreateview(layoutinflater inflater, viewgroup container,
        bundle savedinstancestate) { 
    return inflater.inflate(r.layout.android_fragment, container, false);
 }
}

 

oncreate()
当fragment创建时被调用,你应该初始化一些实用的组件,比如在fragment暂停或停止时需要恢复的

oncreateview()
当系统调用fragment在首次绘制用户界面时,如果画一个ui在你的fragment你必须返回一个view当然了你可以返回null代表这个fragment没有ui.

那么如何添加一个fragment到activity中呢? activity的布局可以这样写

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <fragment android:name="com.android.cwj.articlelistfragment"
   android:id="@+id/list"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:layout_height="match_parent" />
 <fragment android:name="com.android.cwj.articlereaderfragment"
   android:id="@+id/viewer"
   android:layout_weight="2"
   android:layout_width="0dp"
   android:layout_height="match_parent" />
</linearlayout>

 通常地 fragment做为宿主activity ui的一部分, 被作为activity整个view hierarchy的一部分被嵌入. 有2种方法你可以添加一个fragment到activity layout:

一、在activity的layout文件中声明fragment
      你可以像为view一样, 为fragment指定layout属性(sdk3.0以后).
      例子是一个有2个fragment的activity:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
  <fragment android:name="com.example.news.articlelistfragment"
   android:id="@+id/list"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:layout_height="match_parent" />
  <fragment android:name="com.example.news.articlereaderfragment"
   android:id="@+id/viewer"
   android:layout_weight="2"
   android:layout_width="0dp"
   android:layout_height="match_parent" />
 </linearlayout>

<fragment> 中的 android:name 属性指定了在layout中实例化的fragment类.

当系统创建这个activity layout时, 它实例化每一个在layout中指定的fragment,并调用每一个上的oncreateview()方法,来获取每一个fragment的layout. 系统将从fragment返回的 view 直接插入到<fragment>元素所在的地方.

注意: 每一个fragment都需要一个唯一的标识, 如果activity重启,系统可以用来恢复fragment(并且你也可以用来捕获fragment来处理事务,例如移除它.)

有3种方法来为一个fragment提供一个标识:

  • 为 android:id 属性提供一个唯一id.
  • 为 android:tag 属性提供一个唯一字符串.
  • 如果以上2个你都没有提供, 系统使用容器view的id.

二、使用fragmentmanager将fragment添加到一个已存在的viewgroup.

       当activity运行的任何时候, 都可以将fragment添加到activity layout.只需简单的指定一个需要放置fragment的viewgroup.为了在你的activity中操作fragment事务(例如添加,移除,或代替一个fragment),必须使用来自 fragmenttransaction 的api.

可以按如下方法,从你的activity取得一个 fragmenttransaction 的实例:

fragmentmanager fragmentmanager = getfragmentmanager();
fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();

然后你可以使用 add() 方法添加一个fragment, 指定要添加的fragment, 和要插入的view.

examplefragment fragment = new examplefragment();
 fragmenttransaction.add(r.id.fragment_container, fragment); 
fragmenttransaction.commit();

      add()的第一个参数是fragment要放入的viewgroup, 由resource id指定, 第二个参数是需要添加的fragment.一旦用fragmenttransaction做了改变,为了使改变生效,必须调用commit().

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网