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

作者:xyz_lmn 时间:2023-05-17 04:30:59 

  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,如图

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

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

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

Activity、Fragment分别对比下:

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

两个的生命周期很类似,也息息相关。
 
 
创建一个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().

标签:Android,Fragment
0
投稿

猜你喜欢

  • JAVA使用JDBC连接oracle数据库的详细过程

    2021-11-20 19:20:19
  • Android UI之ImageView实现图片旋转和缩放

    2023-08-04 02:53:39
  • C++程序中启动线程的方法

    2023-06-28 03:35:02
  • 大前端代码重构之事件拦截iOS Flutter Vue示例分析

    2021-12-11 20:16:53
  • 简单学习C#中的泛型方法使用

    2022-11-06 19:37:27
  • 使用genymotion访问本地上Tomcat上数据的方法

    2022-11-23 05:51:43
  • SpringBoot获取配置文件内容的几种方式总结

    2023-11-24 18:10:48
  • 浅谈mybatis中SQL语句给boolean类型赋值问题

    2023-01-19 15:15:42
  • SpringBoot实现本地存储文件上传及提供HTTP访问服务的方法

    2022-09-14 19:09:12
  • springboot中bean的加载顺序问题

    2022-01-04 19:55:57
  • SpringBoot项目速度提升之延迟初始化(Lazy Initialization)详解

    2021-11-27 06:37:22
  • Winform窗体圆角设计代码

    2022-04-22 18:15:30
  • 解决mybatis 中collection嵌套collection引发的bug

    2023-03-20 20:55:39
  • Android activity堆栈及管理实例详解

    2022-07-28 01:17:14
  • Java数据结构BFS广搜法解决迷宫问题

    2023-12-03 16:04:33
  • Android Listview多tab上滑悬浮效果

    2021-07-27 22:06:23
  • SpringMVC 向jsp页面传递数据库读取到的值方法

    2022-03-29 00:51:15
  • OpenGL Shader实现阴影遮罩效果

    2022-04-23 19:38:02
  • 使用SpringMVC的@Validated注解验证的实现

    2023-09-20 19:49:55
  • SpringMVC 数据校验方法(必看篇)

    2023-11-14 21:44:05
  • asp之家 软件编程 m.aspxhome.com