android Fragment 使用
一 、Fragment的理解
Fragment 与activity 相似,但比activity 多出几个方法 ,Fragment的生命周期小于activity
一个Activity 中可以包含多个Fragment ,fragment 就像一个个积木,可以组合成一个大的玩具Activity。

activity oncreate onstart onResume onPause onstop ondestroy onRestart
1. onAttach() 当Fragment和Activity建立关联的时候调用。
2. onCreateView() 为Fragment创建视图(加载布局)时调用。
3. onActivityCreated() 确保与Fragment相关联的Activity一定已经创建完毕的时候调用。
4. onDestroyView() 当与Fragment关联的视图被移除的时候调用。
5. onDetach() 当Fragment和Activity解除关联的时候调用。
二、 Fragment 实际使用例子
1、 准备 一个xml 文件
首先,创建一个工程,叫做FragmentTest,然后创建一个left_fragment.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="点我"
/>
</LinearLayout>
2、创建一个LeftFragment 类 继承于 Fragment 类 ,包选择 android.app.Fragment 。 V4 下面的Fragment是为了兼容低版本的安卓系统。
重写 oncreateView() 方法,将 left_fragment.xml 文件添加到布局中
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}
同理,在准备right_fragment
3、 在Activity 中加入fragment 。同样,首先建立 activity_main.xml 文件
?xml version="1.0" encoding="utf-8"?>
<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="horizontal"> <fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
></fragment> <FrameLayout
android:id="@+id/right_framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragmenttest.RightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
></fragment>
</FrameLayout> </LinearLayout>
4、运行程序 ,主页面加载完 activity_main.xml 会显示如下界面。

三、动态加载替换 fragment
上面的例子,我们是在activity_main.xml文件中写死了fragment
1、还是准备好xml文件 other_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#CFEDDB"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="我是另一个的fragment"
/>
</LinearLayout>
2、Othe_Fragment 类
1 public class LeftFragment extends Fragment {
2 @Nullable
3 @Override
4 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
5 View view = inflater.inflate(R.layout.other_fragment, container, false);
6 return view;
7 }
8 }
3、 MainActivity中给按钮设置点击事件,动态添加Fragment的代码逻辑都放在了点击事件中。
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//总共分5步
//创建fragment对象
OtherFragment otherFragment = new OtherFragment();
//获取FragmentManager管理器
FragmentManager fragmentManager = getFragmentManager();
//开启事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//替换碎片文件
fragmentTransaction.replace(R.id.right_framelayout,otherFragment);
//提交事务
fragmentTransaction.commit();
}
});
}
运行如下

android Fragment 使用的更多相关文章
- 【Android自学日记】【转】Android Fragment 真正的完全解析(下)
上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API,如果你还不了解,请看:Android Fragment 真正的完全解析(上). 本篇将介绍上篇博客提到的:如何管理Frag ...
- Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...
- Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复
Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
- Android Fragment使用(一) 基础篇 温故知新
Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...
- Android Fragment应用实战
现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...
- Android Fragment 真正的完全解析(下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...
- Android Fragment
1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...
- Android Fragment完全解析
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...
随机推荐
- Sublime、Webstorm,还有CLI、Atom,这些开发工具的更新你清楚吗?
APICloud App开发平台一直在不断升级开发工具库,这一年增加了众多开发工具.目的就是让开发者可以选择使用任何自己喜欢的HTML5开发工具去开发App. 那么2016年到现在,这些开发工具都有了 ...
- 【杂记】JavaScript篇
35.jquery中ifram子窗体调用父窗体方法.父窗体调用子窗体方法 //调用子窗体中的方法. ].contentWindow;//获取子窗体的window对象 childWindow.subFo ...
- PythonNote01_HTML标签
>头标签<head> >>位置 头标签要放在头部之间 >>种类 <title> : 指定整个网页的标题,在浏览器最上方显示. <meta&g ...
- windows远程控制ubuntu---基于ssh
要实现windows下连接ubuntu需要安装以下软件: 1. windows下安装winSCP 2. Ubuntu下安装OpenSSH Server 可以使用命令行安装openSSH Server: ...
- 无题 MVC
1. MVC 里controller 返回匿名类型, 在View里是访问不了匿名类型的字段,因为它是Internal Private, 必须定义强类型 2. 扩展view的方法 public stat ...
- c语言第2次作业
- iar调试
我们可以自己建立自己的工程了,但这一步只是开发中的第一小步.今天就来说说开发中举足轻重的另外一件事:调试. 其实调试本身也并不难,楼主总结,调试关键在于两件事,一是运行,二是观察,为了更好的实现这两者 ...
- HTML5--拖动02-dragstart、drag、dragenter、dragover、dragleave、drop、dragend属性
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- FORM中需要反复选择LOV
注意:1.字段长度问题 2.提示显示样式:第一条记录 3.那些不现实的返回项,或者是只读的返回项可以 将从列表中验证 改为 否 比如一个LOV 返回2个值 但是其实只用选择前一个就可以带出后一 ...
- c#-二分查找-算法
折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...