Android-------------fragment的学习
一、fragment的静态使用
1.使用步骤:
a.继承fragment 重写onCreatevVew()的回调方法
b.设置Fragment的布局
c.在Activity中声明Fragment 使用方式与控件相同
2.代码展示:
a.继承fragment 重写onCreatevVew()的回调方法
public class TileFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tilefragment, null);
}
}
b.设置Fragment的布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"> //忘记写会有控件覆盖,会报错的 <ImageView
android:id="@+id/iv_bck"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/ic_launcher" /> <TextView
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是标题"
/>
</LinearLayout> c.在Activity中声明Fragment 使用方式与控件相同
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" //这个忘写会报错误,很难找出来的
tools:context=".MainActivity"> <fragment
android:name="com.example.zhang.myapplication.TileFragment" ----------->引用标签来静态加载fragment的数据
android:layout_width="match_parent"
android:layout_height="50dp"></fragment>
</LinearLayout> 二、fragment的动态使用
a.创建Fragment的管理器对象
b.获取Fragment的事物对象并开启
c.调用事物的动态方法:动态的添加、动态的删除、动态的替换
d.提交事物
在xml文件中的代码,在布局文件中写好布局并设置ID,通过FragmentTransation.add去动态添加
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/fragment_tile"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frag_content"
/>
//获得FragmentManager的对象
FragmentManager manager = getSupportFragmentManager();
//获取Fragment的事物对象并开始
FragmentTransaction transaction = manager.beginTransaction();
//动态添加fragment (add remove replace)
transaction.add(R.id.fragment_tile, new TileFragment());
transaction.add(R.id.frag_content, new ContentFragment());
//提交获得的事物
transaction.commit(); 三、Fragment的生命周期
1.第一次启动app的时候
onAttach:activity与fragment的产生关联时候进行回调
onCreateView : 用户第一次绘制界面
onActivityCreate : fragment与activity创建成功进行回调

2.切换其他fragment的时候

3.返回第一次启动的fragment的时候

4.按home键的时候

5.home键之后再重新返回当前界面

6.按back键的时候

7.fragment和activity的生命周期

Android-------------fragment的学习的更多相关文章
- android fragment 博客 学习记录
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...
- Android Fragment应用实战
现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...
- 【转】基于Android Fragment功能的例子
原文网址:http://blog.csdn.net/eyu8874521/article/details/8252216 通过最近空闲时候对Fragment的学习,尝试着写了一个小Demo,将在开发的 ...
- 基于Android Fragment功能的样例
通过近期空暇时候对Fragment的学习,尝试着写了一个小Demo,将在开发的时候能经常使用到的Fragment知识放在一起,写过了这个Demo对Android Fragment的了解更加深入了,以后 ...
- Android编程权威指南笔记3:Android Fragment讲解与Android Studio中的依赖关系,如何添加依赖关系
Android Fragment 当我在学习时,了解了Fragment词汇 Fragment是一种控制器对象,我就把所了解的简单说一下.activity可以派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 ...
随机推荐
- 批量MD5命名文件
#coding=utf-8 import os import hashlib def GetFileMd5(filename): if not os.path.isfile(filename): pr ...
- VBA json parser[z]
http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html VB-JSON: A Visual Basic 6 (VB ...
- OSGi 系列(六)之服务的使用
OSGi 系列(六)之服务的使用 1. 为什么使用服务 降低服务提供者和服务使用者直接的耦合,这样更容易重用组件 隐藏了服务的实现细节 支持多个服务的实现.这样你可以互换这实现 2. 服务的使用 2. ...
- 任务取消TASK
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- filter 死循环(tomcat 启动完成 ,自动执行filter.dofilter,导致tomcat 启动超时) , tomcat 启动和 servers 启动 不同
package com.diancai.interceptor; import java.io.IOException; import javax.servlet.Filter; import jav ...
- struts2乱码问题
简介:做了个功能,用的struts2,表单提交到后台,接收后打印出来的数据乱码. 解决步骤: 1. struts.xml中配置<constant name="struts.i18n ...
- CountVectorizer()类解析
主要可以参考下面几个链接: 1.sklearn文本特征提取 2.使用scikit-learn tfidf计算词语权重 3.sklearn官方中文文档 4.sklearn.feature_extra ...
- SharedPreferences的基本使用-----存,删,改,查
1.创建一个SharedPreferences对象 SharedPreferences spf = context.getSharedPreferences("imageload" ...
- 2018.10.15 loj#6010. 「网络流 24 题」数字梯形(费用流)
传送门 费用流经典题. 按照题目要求建边. 为了方便我将所有格子拆点,三种情况下容量分别为111,infinfinf,infinfinf,费用都为validi,jval_{id_{i,j}}valid ...
- 2018.10.12 NOIP模拟 数据结构(线段树)
传送门 sb线段树题居然还卡常. 修改操作直接更新区间最小值和区间标记下传即可. 询问加起来最多5e65e65e6个数. 因此直接询问5e65e65e6次最小值就行了. 代码