Android App组件之Fragment说明和示例
Android App组件之Fragment说明和示例
1 Fragement介绍
Android从3.0开始引入Fragment,主要是为了支持更动态更灵活的界面设计。
Fragment是activity的界面中的一部分或一种行为。你可以把多个Fragment们组合到一个activity中来创建一个多面界面,你也可以在多个activity中重用一个Fragment。你可以把Fragment认为模块化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除。
需要注意的是:Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响。例如:当activity暂停时,它拥有的所有的Fragment们都暂停了,当activity销毁时,它拥有的所有Fragment们都被销毁。
2 Fragment生命周期
3 Fragment实现方法
一般情况下,fragment把它的layout作为activitiy的loyout的一部分合并到activity中,有两种方法将一个fragment添加到activity中。
方法1:在activity的layout.xml文件中声明fragment
下面以一个应用实例来说明方法1。应用实例要求:创建一个activity,activity中包含Fragment。
layout文件fragment_layout_test.xml的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <fragment android:name="com.skywang.app.FragmentLayoutTest$ExampleFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>
说明:
(01) 该layout仅仅只包含了一个fragment元素。
(02) fragment的属性 android:name="com.skywang.app.FragmentLayoutTest$ExampleFragment" 。意思是该layout包含一个fragment,这个fragment是定义在包名为"com.skywang.app"中类"FragmentLayoutTest""的内部类"ExampleFragment"中的。
FragmentLayoutTest.java的代码如下:
package com.skywang.app; import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.app.Fragment; public class FragmentLayoutTest extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fragment_layout_test只包括ExampleFragment对象。
setContentView(R.layout.fragment_layout_test);
} // 继承与Fragment
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.example_fragment, container, false);
}
}
}
说明:
(01) FragmentLayoutTest通过 setContentView(R.layout.fragment_layout_test) 来调用fragment_layout_test.xml来作为布局文件。
(02) 而上面的fragment_layout_test.xml布局文件仅仅值包括1个Fragment,该Fragment是通过ExampleFragment实现的。
(03) ExampleFragment继承于Fragment,在onCreateView()方法中通过 return inflater.inflate(R.layout.example_fragment, container, false); 来将example_fragment.xml作为其布局文件。
所以,相当于FragmentLayoutTest直接调用example_fragment.xml来显示。那为什么要费这么大劲,非要用到Fragment呢?原因是因为Fragment的可扩展性,它能作为一个独立的显示单元添加到activity中。本文仅仅只是为了说明fragment的layout实现方法;实际应用中,可能比这复杂很多。
layout文件example_fragment.xml的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:text="@string/example_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>
说明:
(01) 采用LinearLayout线性布局,仅仅显示一个TextView。
点击下载:源代码
效果图:
方法2:在activity的layout.xml文件中声明包含
下面以一个应用实例来说明方法1。应用实例要求:创建一个activity,activity中包含一个Fragment,该Fragment采用动态加载的方式。
FragmentTransactionTest的代码:
package com.skywang.app; import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction; public class FragmentTransactionTest extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_transaction_test); // 获取FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// 获取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // 获取ExampleFragment
ExampleFragment fragment = new ExampleFragment();
// 将fragment添加到容器about_fragment_container中
fragmentTransaction.add(R.id.about_fragment_container, fragment);
fragmentTransaction.commit();
} // 继承与Fragment
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.example_fragment, container, false);
}
}
}
说明:
(01) 在onCreate()中该activit调用fragment_transaction_test作为其布局文件。
(02) 在onCreate()中,通过获取FragmentManager和FragmentTransaction,来将ExampleFragment对象添加到R.id.about_fragment_container中,about_fragment_container是一个Fragment。
(03) ExampleFragment的onCreateView()中,将example_fragment作为其布局文件。
fragment_transaction_test.xml的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <FrameLayout
android:id="@+id/about_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>
example_fragment.xml的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:text="Hello Fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>
点击下载:源代码
效果图:
Android App组件之Fragment说明和示例的更多相关文章
- Android App组件之ListFragment -- 说明和示例
Android App组件之ListFragment -- 说明和示例 1 ListFragement介绍 ListFragment继承于Fragment.因此它具有Fragment的特性,能够作为a ...
- Android App组件之ListFragment -- 说明和示例(转载)
转自:http://www.cnblogs.com/skywang12345/p/3160260.html 1 ListFragement介绍 ListFragment继承于Fragment.因此它具 ...
- Android App组件之Activity
Android App组件之Activity 1 activit介绍 Activities 是Android的四大组件之一,其余三大组件是service.broadcast和content provi ...
- Android APP架构设计——MVP的使用示例
0. 前言 为了更好地进行移动端架构设计,我们最常用的就是MVC.MVP和MVVM,作为三个最耳熟能详的三大架构,应用可谓非常广泛.对于这三种架构设计以及优缺点已经在Android APP架构设计-- ...
- Android做法说明(3)---Fragment使用app袋或v4包解析
Android做法说明(3)---Fragment使用app袋或v4包解析 1)问题简述 相信非常多的朋友在调用Fragment都会遇到以下的情况: watermark/2/text/aHR0cDov ...
- android开发之路12(android四大组件&Fragment&AsyncTask类)
一.Activity组件1.简介:Activity组件是Android四大组件之一,通常一个Activity相当于一个用户界面,我们可以通过加载布局文件将Android提供的各种控件及自定义控件显示到 ...
- App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南
面对越来越复杂的 App 需求,Google 官方发布了Android 框架组件库(Android Architecture Components ).为开发者更好的开发 App 提供了非常好的样本. ...
- android.support.v4.app.Fragment和android.app.Fragment区别
1.最低支持版本不同 android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版 android.support.v ...
- android四大组件学习总结以及各个组件示例(2)
上篇博文讲解了activity.content provider,此篇博文来仔细总结service.broadcast receiver: 3. Service >什么是服务?>windo ...
随机推荐
- ssh命令集锦
[前提] ssh命令其实平时工作会比较少能够用到(因为直接用远程客户端来连接) 但是偶尔还是需要利用ssh临时的连接到某个服务器,所以当遇到的时候来总结一下 [集锦] 一.ssh以某个用户名连接到某个 ...
- 由time.tzname返回值引发的对str、bytes转换时编码问题实践
Windows 10家庭中文版,Python 3.6.4, 下午复习了一下time模块,熟悉一下其中的各种时间格式的转换:时间戳浮点数.struct_tm.字符串,还算顺利. 可是,测试其中的time ...
- Android实现手机摄像头的自动对焦
如何实现Android相机的自动对焦,而且是连续自动对焦的.当然直接调用系统相机就不用说了,那个很简单的.下面我们主要来看看如如何自己实现一个相机,并且实现自动连续对焦. 代码如下: public c ...
- javaweb笔记六
指令包含:可以在一个jsp中包含另一个jsp中的内容.会将包含页面和被包含页面放在一起编译,形成一个java类.所以,是在编译时发生的.只能包含文件,不允许两个页面之间存在同名变量.被包含页面也不应该 ...
- hdu 5427(排序水题)
排序 年轻的排前面 名字中可能有空格 Sample Input21FancyCoder 19962FancyCoder 1996xyz111 1997 Sample OutputFancyCoderx ...
- Mysql 查看用户
一.查看用户和允许用户登录的地址权限. use mysql select host,user from user; 二.查看用户和所有用户权限 select * from user \G
- 高能天气——团队Scrum冲刺阶段-Day 7 总结
高能天气--团队Scrum冲刺阶段-Day 7 总结 今日完成任务 于欣月:修改项目说明书,帮助修改应用 余坤澎:进行应用整合的收尾工作 康皓越:进行应用整合的收尾工作 范雯琪:修改项目说明书,完成项 ...
- 002.etcd使用场景
引用链接: https://blog.csdn.net/linuxheik/article/details/77853119 https://www.cnblogs.com/doscho/p/6221 ...
- IntellijIDEA快速入门(Windows版)
跟随公司变更技术堆栈的步伐,开始学习相应工具IntelliJ的使用,之前一个大神同时也提到,最近该IDE的市场份额已然超越了免费的Eclipse,因此该工具已经到了必须会的程度了. 新年快乐,鸡年大吉 ...
- 领英Linkedin信息搜集工具InSpy
领英Linkedin信息搜集工具InSpy 领英Linkedin是一个知名职业社交媒体网站.通过该网站,渗透测试人员可以获取公司内部组成和员工信息.Kali Linux提供一款专用的信息收集工具I ...