Android中Fragment的简单介绍
Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片段或者成为碎片(个人理解),可以把Fragment当成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。
其中有个经典图,大家就字面上理解下就行:
可以把Fragment设计成可以在多个Activity中复用的模块,为了在Android上创建动态的、多窗口的用户交互体验,你需要将UI组件和Activity操作封装成模块进行使用,在activity中你可以对这些模块进行切入切出操作。可以使用Fragment来创建这些模块,如果一个fragment定义了自己的布局,那么在activity中它可以与其他的fragments生成不同的组合,从而为不同的屏幕尺寸生成不同的布局(一个小的屏幕一次只放一个fragment,大的屏幕则可以两个或以上的fragment),比如说下图:
Fragment的两种显示方式
首先,新建一个布局文件fragmentcontent.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:src="@drawable/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </FrameLayout>
这时候与对应的是建立一个显示布局文件的ContentFragment类:
public class ContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragmentcontent, container, false);
}
}
inflate()方法的三个参数:
第一个是resource ID,指明了当前的Fragment对应的资源文件;
第二个参数是父容器控件;
第三个布尔值参数表明是否连接该布局和其父容器控件,在这里的情况设置为false,因为系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View Group。
如果需要在Mainactivity中显示的话:
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmenttest.ContentFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
activity_main.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"
tools:context="com.example.fragmenttest.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中共十八大四中全会召开" /> <fragment
android:id="@+id/fragment1"
android:name="com.example.fragmenttest.ContentFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="依法治国,以人为本" /> <LinearLayout
android:id="@+id/parentTest"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
显示的结果为:
这个时候可以在Mainactivity中加一段代码:
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
ContentFragment fragment = new ContentFragment();
fragmentTransaction.add(R.id.parentTest, fragment);
fragmentTransaction.commit();
结果如下:
FragmentManager操作Fragment
基本操作的就是添加,替换,删除(如果是定义在xml文件中的是不可以删除的)
自定义的添加:
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
ContentFragment fragment = new ContentFragment();
fragmentTransaction.add(R.id.parentTest, fragment);
fragmentTransaction.commit();
替换:
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
TitleFragment fragment = new TitleFragment();
fragmentTransaction.replace(R.id.fragment1, fragment);
fragmentTransaction.commit();
如果仔细看一下,上面应该还是可以看出来基本上操作就是获取Manager,然后就行添加,删除操作,关于内部Fragments之间的交互还在研究中,希望有机会可以补发~
Android中Fragment的简单介绍的更多相关文章
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android开发——Fragment的简单使用总结
前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android 中Fragment使用
Android 中Fragment使用 public class MainActivity extends Activity { public static String[] array = { &q ...
- [原创]Android中LocationManager的简单使用,获取当前位置
Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...
- Android中Fragment的两种创建方式
fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...
- Android中ProgressDialog的简单示例
网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...
- Android中View和ViewGroup介绍
1. 概念Android中的View与我们以前理解的“视图”不同.在Android中,View比视图具有更广的含义,它包含了用户交互和显示,更像Windows操作系统中的window. ViewGro ...
- Android中fragment之间和Activity的传值、切换
功能介绍:通过一个activity下方的三个按钮,分别是发送消息(sendButton).聊天记录(chatButton).常用语(commonButton).当单击按钮是,来切换上方的fragmen ...
随机推荐
- SKLearn数据集API(二)
注:本文是人工智能研究网的学习笔记 计算机生成的数据集 用于分类任务和聚类任务,这些函数产生样本特征向量矩阵以及对应的类别标签集合. 数据集 简介 make_blobs 多类单标签数据集,为每个类分配 ...
- 【BZOJ 2337】 2337: [HNOI2011]XOR和路径(概率DP、高斯消元)
2337: [HNOI2011]XOR和路径 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1170 Solved: 683 Description ...
- navicat premium 的使用——navicat 连接MySQL数据库
最近,在学习navicat premium这款第三方软件,他是一个三方数据库管理软件,在前一段时间实习的时候,我使用这款软件调用过服务器上的数据库,不过,当时并没有仔细研究,再次遇到这个软件,我决定仔 ...
- 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution
题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...
- Vue-router浅析(二)
一.导航守卫 经常在项目中,会对页面做一些权限控制,所以需要对路由变化做出处理,跳转或取消相应的路由. 导航:即路由发生变化.导航守卫即对路由变化做出处理.当然也分几种情况:对全局变化做处理.对&qu ...
- BZOJ 2750 HAOI 2012 Road 高速公路 最短路
题意: 给出一个有向图,求每条边有多少次作为最短路上的边(任意的起始点). 范围:n <= 1500, m <= 5005 分析: 一个比较容易想到的思路:以每个点作为起点,做一次SPFA ...
- VC 调用 Python
//file:py.h BOOL InitPython(); BOOL ClosePython(); ======================== //file:py.cpp #include & ...
- cdoj 841 休生伤杜景死惊开 逆序数/树状数组
休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 陆伯言军陷八卦 ...
- jProfiler远程连接Linux监控jvm的运行状态
第一步:下载软件官网地址:https://www.ej-technologies.com/download/jprofiler/files,下载一个linux服务端,一个windows客户端 GUI界 ...
- 二值化方法:Kittler:Minimum Error Thresholding
Kittler二值化方法,是一种经典的基于直方图的二值化方法.由J. Kittler在1986年发表的论文“Minimum Error Thresholding”提出.论文是对贝叶斯最小错误阈值的准则 ...