在前两篇博文中分别介绍了Fragment得基础和Fragment的生命周期,然而说了这么多Fragment到底怎么用呢以及我们为什么要使用Fragment?本篇博文将主要探讨这两个问题,首先说下在APP中有这好好Activity,跳转起来有那么简单,我们为什么还要使用Fragment呢?这是因为Fragment相对Activity而言更加的轻量级,使用起来也更加灵活,在一个程序的内部界面切换,尽可能的用Fragment代替Activity会让我们的APP运行起来更加的流畅,更加的高效,同时也提高了界面的复用性。而却Fragment在适应多尺寸屏幕方面表现也非常优秀。

  首先看一下栗子,非常简单的一个小示例,效果图如下:

    

  体验一下就会发现,两个Fragment跳转起来要比Activity跳转的速度快很多。

  MainActivity.java代码如下:

 /**
* MainActivity 主界面
* @author codingblock 2015/09/14
*
*/
public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
}
}

  在MainActivity首先通过getSupportFragmentManager()方法获取FragmentTransaction的对象,然后用add()方法将MainFragment加载进来,其中引用的布局文件activity_main.xml如下:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.codingblock.learnfragment.MainActivity"
tools:ignore="MergeRootFrame" />

  下面是MainFragment的代码:

 /**
* MainFragment 主Fragment
* @author codingblock 2015/09/14
*
*/
public class MainFragment extends Fragment { public MainFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
rootView.findViewById(R.id.btn_show_other).setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
getFragmentManager()
.beginTransaction()
.addToBackStack(null) //将当前fragment加入到返回栈中
.replace(R.id.container, new OtherFragment()).commit();
}
});
return rootView;
}
}

  在这个Fragment放了一按钮用于跳转到另一个Fragment,然后通过FragmentTransaction对象的replace()方法让OtherFragment把当前Fragment替换掉,在这里需要注意的是,如果想让程序可以通过后退方式显示上一个Fragment的话,需要在替换之前通过addToBackStack()把当前Fragment加入到返回栈中。

  它的布局文件fragment_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.codingblock.learnfragment.MainActivity$PlaceholderFragment" > <Button
android:id="@+id/btn_show_other"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="跳转到另一个Fragment" /> </LinearLayout>

  最后一个OtherFragment代码如下:

 /**
* OtherFragment 另一个Fragment
* @author codingblock 2015/09/14
*
*/
public class OtherFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_other, container, false);
rootView.findViewById(R.id.btn_back).setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
//从栈中将当前fragment推出
getFragmentManager().popBackStack();
}
});
return rootView;
}
}

  程序跳转到这个Fragment之后,如果想返回上一个MainFragment我们可以点击后退键,也可以为一个按钮绑定一个单击事件用FragmentTransaction的popBackStack()方法将当前的Fragment推出栈即可。

  它的布局文件代码如下:

 <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" > <TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是另一个Fragment" /> <Button
android:id="@+id/btn_back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="返回" /> </LinearLayout>

  到此为止,栗子就结束了,虽然很简单,却能很清楚的说明Fragment的用法。

  声明:欢迎转载,转载时请附上本文链接。

  

Android学习笔记(七)两个Fragment简单跳转示例的更多相关文章

  1. Android学习笔记(五)Fragment简介

    Fragment是在Android 3.0 (API level 11)中引入的Activity的子模块.初衷是为了适应大屏幕的平板电脑,我们只需要使用Fragment对UI组件进行分组.模块化管理, ...

  2. Android学习笔记七:五大存储

    在Android中,可供选择的存储方式有SharedPreferences.文件存储.SQLite数据库方式.内容提供器(Content provider)和网络. 一.SharedPreferenc ...

  3. Android学习笔记(六)Fragment的生命周期

    在上一篇博文中对Fragment做了简单的介绍,现在再来探讨一下Fragment的生命周期. 一.Fragment的几种状态: 与Activity类似,Fragment也有一下几种状态: · 活动状态 ...

  4. Android学习笔记—Windows下NDK开发简单示例

    该示例假设Android开发环境已经搭建完成,NDK也配置成功: 1.在Eclipse上新建Android工程,名称为ndkdemo.修改res\layout\activity_main.xml &l ...

  5. android学习笔记七——控件(DatePicker、TimePicker、ProgressBar)

    DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础 ...

  6. Android 学习笔记之 个人认为最简单的查看Android源码方案

    相信很多人都会疑惑如何使用Eclipse ADT查看源码? 下面我们将介绍 如何查看Android源码. 本文有如下优点: 1.不用费心去找Android源码地址:一个字烦,网上的东西杂七杂八的... ...

  7. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  8. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  9. 【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件

    目录(?)[-] 在AIDL中定义服务接口 根据AIDL文件自动生成接口代码 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.n ...

随机推荐

  1. Animating Layout Changes(展开收起)

    原文地址:https://developer.android.com/training/animation/layout.html#add (1)设置布局文件: <LinearLayout an ...

  2. 不安装oracle客户端,连接到服务器的oracle (注:针对 odp.net)

    前几天在研究怎样不安装oracle客户端去访问oracle,并把里面的数据同步到本地的Sql Server数据库中. 准备工作:首先你得有如下.dll,我这个是针对oracle10g的,如果是更高的版 ...

  3. SVM对偶形式

    dual svm 对偶SVM linear SVM 可以用二次规划方法解 xn通过非线性转换变成zn SVM配合非线性特征转换 透过large-margin降低模型复杂度 透过特征转换得到弯弯曲曲的边 ...

  4. Java学习----Math函数

    public class TestMath { public static void main(String[] args) { System.out.println(Math.E); System. ...

  5. django添加静态文件

    最近做了一个todolist webapp,需要稍微添加css时候又忘记django的添加方法了,查看了以前的项目才想起来,所以记录一下. 1.settings.py 将以下代码放到最下面 STATI ...

  6. 关于移动div的具体实现(js)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. LICAppInfo

    [Common] Enabled=true All=true [AppList] #AppName                       #Enabled            #Validit ...

  8. ES5中数组新增的方法说明

    一.前言-索引 ES5中新增的不少东西,了解之对我们写JavaScript会有不少帮助,比如数组这块,我们可能就不需要去有板有眼地for循环了. ES5中新增了写数组方法,如forEach (js v ...

  9. Log4Net配置注意点

    log4Net的配置文章一搜一大把,配置使用还是有一些点花费了很多时间,这里整理一下,添上坑,让Developer走的更稳. 编程式配置路径 新建一个配置文件,通过写代码来动态加载log4Net的配置 ...

  10. linux socket使用经验总结

    1.  scoket函数中PF_INET AF_INET区别 在UNIX系列中,PF_INET表示poxis, BSD系列用AF_INET 2.  in_addr_t inet_addr(const ...