Android之Fragment学习笔记①
Android Fragment完全解析,关于碎片你所需知道的一切
一. 什么是Fragment
Fragment(碎片)就是小型的Activity,它是在Android3.0时出现的。Fragment是表现Activity中UI的一个行为或者一部分。
可以把fragment想象成activity的一个模块化区域,有它自己的生命周期,接收属于它自己的输入事件,并且可以在activity运行期间添加和删除(有点像一个可以在不同的activity中重用的“子Activity”)。
Fragment必须被嵌入到一个activity中。它们的生命周期直接受其宿主activity的生命周期影响。当一个activity正在运行时,就可以独立地操作每一个Fragment,比如添加或删除它们。
Fragment可以定义自己的布局、生命周期回调方法,因此可以将fragment重用到多个activity中,因此可以根据不同的屏幕尺寸或者使用场合改变fragment组合。
二. 如何创建一个Fragment
1、为Fragment定义一个布局
2、定义类继承Fragment
3、重写类中的onCreateView方法,返回一个View对象作为当前Fragment的布局。
fragment第一次绘制它的用户界面的时候,系统会调用onCreateView()方法。为了绘制fragment的UI,此方法必须返回一个作为fragment布局的根的view。如果fragment不提供UI,可以返回null。
代码:如Fragment01和Fragment02所示。
三. 如何将Fragment添加到Activity
Activity必须在清单文件中进行声明,但是Fragment不需要,Fragment只需要在Activity的布局文件layout_main.xml中声明就可以了。
<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.Fragment01"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentdemo.Fragment02"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>
Fragment的代码:
package com.example.fragmentdemo; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by gary on 2016/4/12.
*/
public class Fragment01 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment1,container,false);
}
} package com.example.fragmentdemo; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by gary on 2016/4/12.
*/
public class Fragment02 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment1,container,false);
}
}
Fragment的布局文件
fragment1.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"
android:background="#00ff00"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个Fragment"
android:textColor="#ff0000"
android:textSize="25sp"/> </LinearLayout> ------------------------
fragment2.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"
android:background="#ff0000"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个Fragment"
android:textColor="#00ff00"
android:textSize="25sp"/> </LinearLayout>
Activity代码:
package com.example.fragmentdemo; import android.app.Activity;
import android.os.Bundle; /**
* Created by gary on 2016/4/12.
*/
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
}
}
效果:
http://i.cnblogs.com/EditPosts.aspx?postid=5380639
注意:代码中的四个属性是必须的要给的,“android:name”属性:指定了在layout中实例化的Fragment类是哪个。
当系统创建这个activitylayout时,它实例化每一个在layout中指定的Fragment,并调用它们的onCreateView()方法,来获取每一个Fragment的layout,系统将从Fragment返回的View直接插入到<fragment>元素所在的地方。
四. 如何动态何切换Fragment
要在Activity中管理Fragment,需要四步
1. 获取FragmentManger对象,在Activity可以通过getFragementManager()来获取实例。
//1.获取Fragment管理器对象
FragmentManager manager = getFragmentManager();
2.开启一个事务,通过调用beginTransaction方法开启。
//2. 开启事务
FragmentTransaction transaction = manager.beginTransaction();
3.向容器中加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。
//3. 将FrameLayout控件替换成Fragment对象
transaction.replace(R.id.frame, new GamesFragment());
4. 提交事务,调用commit方法提交。
//4. 提交事务
transaction.commit();
案例:点击不同的按钮切换到不同的Fragment进行显示。
具体实现步骤:
1. 设置布局文件layout_main.xml中添加三个按钮用于切换Fragment,并在按钮下方添加一个FrameLayout用来替换成相应的Fragment布局。
<?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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="新闻"
android:onClick="news"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="体育"
android:onClick="sports"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="游戏"
android:onClick="games"/> </LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frame"/> </LinearLayout>
2. 创建Fragment的布局文件,fragment_news.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"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="新闻栏目"
android:textSize="28sp"
android:textColor="#0000ff"/>
</LinearLayout>
fragment_sports.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="体育栏目"
android:textSize="28sp"
android:textColor="#ff0000"/>
</LinearLayout>
fragment_games.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="游戏栏目"
android:textSize="28sp"
android:textColor="#00ff00"/>
</LinearLayout>
3. 创建三个Fragment,SportsFragment、NewsFragment、GameFragment。
public class NewsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_news,null);
}
}
SportFragment和GamesFragment中代码和NewsFragment相似。
4. 添加切换Fragment的逻辑,分别添加新闻、体育、游戏的点击事件。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
}
public void news(View v){
//获取Fragment管理器对象
FragmentManager manager = getFragmentManager();
//开启事务
FragmentTransaction transaction = manager.beginTransaction();
//将FrameLayout控件替换成Fragment对象
transaction.replace(R.id.frame, new NewsFragment());
//提交事务
transaction.commit();
}
public void games(View v){
//获取Fragment管理器对象
FragmentManager manager = getFragmentManager();
//开启事务
FragmentTransaction transaction = manager.beginTransaction();
//将FrameLayout控件替换成Fragment对象
transaction.replace(R.id.frame, new GamesFragment());
//提交事务
transaction.commit();
}
public void sports(View v){
//获取Fragment管理器对象
FragmentManager manager = getFragmentManager();
//开启事务
FragmentTransaction transaction = manager.beginTransaction();
//将FrameLayout控件替换成Fragment对象
transaction.replace(R.id.frame, new SportsFragment());
//提交事务
transaction.commit();
}
}
sports()方法、games()方法同上
5. 运行效果

Android之Fragment学习笔记①的更多相关文章
- [android]p7-1 fragment学习笔记
本文源自<android权威编程指南第3版>第7章UI fragment与fragment 第7章主要内容是实现一个记录不良行为的APP(部分实现),有列表,有具体的行为内容显示.第7章主 ...
- Android之Fragment学习笔记②(Fragment生命周期)
一. Fragment生命周期图 二.Fragment生命周期方法介绍 Fragment的生命周期和activity生命周期很像,其生 ...
- Android安装器学习笔记(一)
Android安装器学习笔记(一) 一.Android应用的四种安装方式: 1.通过系统应用PackageInstaller.apk进行安装,安装过程中会让用户确认 2.系统程序安装:在开机的时候自动 ...
- Android应用开发学习笔记之Fragment
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Fragment翻译成中文就是“碎片”.“片断”的意思,Fragment通常用来作为一个Activity用户界面的一 ...
- android cocos2d-x for Android安装和学习笔记(请用adt-bundle21.1或以上导入)
引用:http://weimingtom.iteye.com/blog/1483566 (20121108)注意:这篇文章用cdt编译ndk工程的内容已过时(现在可以用adt-bundle,避免配置繁 ...
- 33.Android之Fragment学习
Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...
- Fragment 学习笔记(1)
网上关于Fragment相关的博客资料很多,写关于这个知识笔记是加深记忆,大神略过: 0x01 了解Fragment 当然看官方文档(http://www.android-doc.com/refere ...
- Fragment学习笔记
Fragment为大量型号,尺寸,分辨率的设备提供了一种统一的UI优化方案.将Activity分解为多个Fragment,将极大地提高UI的灵活性,也更容易为一些新的设备配置带来更好的用户体验. on ...
- Android Fragment学习笔记(二)----Fragment界面添加和管理
Fragment界面添加 了解过fragment的生命周期等简单知识,于是去看官方文档来了解更多相关内容,要添加fragment到我们的UI界面中,给出了两种常用的方法,第一个是在activity的布 ...
随机推荐
- JS中使用EL表达式
转自:http://blog.csdn.net/monkeyking1987/article/details/17146951 分两种情况 1. JS代码在JSP页面中, 这可以直接使用EL表达式. ...
- Android ViewHolder的作用与用法
就是一个持有者的类,他里面一般没有方法,只有属性,作用就是一个临时的储存器,把你getView方法中每次返回的View存起来,可以下次再用.这样做的好处就是不必每次都到布局文件中去拿到你的View,提 ...
- HBase伪分布式环境下,HBase的API操作,遇到的问题
在hadoop2.5.2伪分布式上,安装了hbase1.0.1.1的伪分布式 利用HBase的API创建个testapi的表时,提示 Exception in thread "main&q ...
- Seismic Unix的一些历史
本文是我从官网上拷贝过来的,上国外网越来越慢了……(离题了). At the Society of Exploration Geophysicists (SEG) Annual Meeting in ...
- MP3/视频播放
简单的视频.MP3播放 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equ ...
- flst与fitem命令是这么用的
k,94,75.69947,44.09817,-40 k,95,50.9349,44.01963,-40 k,96,37.83799,45.04814,-40 k,97,15.1304,48.2934 ...
- BZOJ1109 : [POI2007]堆积木Klo
f[i]表示第i个在自己位置上的最大值 则f[i]=max(f[j])+1 其中 j<i a[j]<a[i] a[i]-a[j]<=i-j -> j-a[j]<=i-a[ ...
- Ural 1018 (树形DP+背包+优化)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 题目大意:树枝上间连接着一坨坨苹果(不要在意'坨'),给 ...
- POJ 1947 (树形DP+背包)
题目链接: http://poj.org/problem?id=1947 题目大意:树中各点都由一条边连接.问要弄出个含有m个点的(子)树,至少需要截去多少条边. 解题思路: 设dp[i][j]为i总 ...
- 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)
http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...