一、基本概念

  • Fragment是依赖于Activity的,不能独立存在的。
  • 一个Activity里可以有多个Fragment。
  • 一个Fragment可以被多个Activity重用。
  • Fragment有自己的生命周期,并能接收输入事件。
  • 我们能在Activity运行时动态地添加或删除Fragment。

Fragment的优点:

  • 模块化(Modularity):只需要把代码写在各自的Fragment中。
  • 可重用(Reusability):多个Activity可以重用一个Fragment。
  • 可适配(Adaptability):根据硬件的屏幕尺寸、屏幕方向,能够方便地实现不同的布局。

二、添加到Activity方法

1、静态方法:

创建各个Fragment.xml添加各自组件,然后创建对应的Fragment类并继承自Fragment,重写OnCreateView觉得Fragment的布局

public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}

再在activity_main.xml中加入使用使用android:name前缀来引用各个Fragment,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" > <fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentdemo.Fragment1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentdemo.Fragment2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>

主MainActivity自动生成。

2、动态添加

保留最外层的线性布局管理器,为其添加一个id

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" > </LinearLayout>

在MainActivity中修改:

public class MainActivity extends Activity {  

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
if (display.getWidth() > display.getHeight()) {
Fragment1 fragment1 = new Fragment1();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();
} else {
Fragment2 fragment2 = new Fragment2();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();
}
} }

首先获取屏幕的宽度和高度,然后进行判断,如果屏幕宽度大于高度就添加fragment1,如果高度大于宽度就添加fragment2。动态添加Fragment主要分为4步:

1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。

2.开启一个事务,通过调用beginTransaction方法开启。

3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。

4.提交事务,调用commit方法提交。

只有activity可以使用WindowManager,否则应该使用Context.getResources().getDisplayMetrics()来获取。

Context.getResources().getDisplayMetrics()依赖于手机系统,获取到的是系统的屏幕信息;

WindowManager.getDefaultDisplay().getMetrics(xx)是获取到Activity的实际屏幕信息。

三、Fragment通信

尽管 Fragment 是作为独立于 Activity的对象实现,并且可在多个 Activity 内使用,但Fragment 的给定实例会直接绑定到包含它的 Activity。具体地说,Fragment 可以通过 getActivity() 访问 Activity实例,并轻松地执行在 Activity 布局中查找视图等任务。

View listView = getActivity().findViewById(R.id.list);

同样地,Activity 也可以使用 findFragmentById() 或 findFragmentByTag(),通过从 FragmentManager 获取对 Fragment 的引用来调用Fragment中的方法。

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

文章来源:https://www.cnblogs.com/cr330326/p/5712022.html

最新文章

  1. 踢爆IT劣书出版黑幕——由清华大学出版社之《C语言入门很简单》想到的(1)
  2. Cassandra 介绍
  3. Mac下命令行中用sublime打开指定文件 设置方法
  4. CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)
  5. Spring In Action(第三版)读书笔记
  6. mysql日期时间函数2
  7. [jQuery编程挑战]008 生成逗号分隔数字
  8. CSU1315:全场最水题之陈兴老师与比赛
  9. 【转载】【时序约束学习笔记1】Vivado入门与提高--第12讲 时序分析中的基本概念和术语
  10. Linux Swap交换分区探讨
  11. Magento 2 Plugin - Interceptor - Magento 2插件 - 拦截器-插件开发
  12. jsp页面上的下拉框案例(Struts2)
  13. ACCESS 查询重复记录
  14. powershell脚本:你的文件已经被黑客篡改.ps1
  15. Java 中12个原子操作类
  16. 13.vue组件
  17. IDEA spirng boot @Autowired注解 mapper出现红色下划线解决方法
  18. python爬虫入门---第三篇:自动下载图片
  19. Web App 和 Native App,哪个是趋势?
  20. 力扣(LeetCode)219. 存在重复元素 II

热门文章

  1. LeetCode 916. Word Subsets
  2. docz 强大简单的文档管理工具
  3. js处理事件冒泡(兼容写法)
  4. [已解决] Python logging 重复打印日志信息
  5. Nexus OSS私服仓库的备份与迁移
  6. GSEA 基因集富集分析
  7. plsql excel导入报错:未发现数据源名称并且未指定默认驱动程序
  8. 自己动手实现DNS协议
  9. C# 序列化与反序列化之Binary与Soap无法对泛型List&lt;T&gt;进行序列化的解决方案
  10. 七年老运维实战中的 Shell 开发经验总结【转】