1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6.  
  7. <FrameLayout
  8. android:id="@+id/container"
  9. android:layout_width="fill_parent"
  10. android:layout_height="0dp"
  11. android:layout_weight="1"/>
  12.  
  13. <LinearLayout
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:orientation="horizontal">
  17. <Button
  18. android:id="@+id/a"
  19. android:onClick="click"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_weight="1"
  23. android:text="Fragment A"/>
  24. <Button
  25. android:id="@+id/b"
  26. android:onClick="click"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. android:text="Fragment B"/>
  31. </LinearLayout>
  32.  
  33. </LinearLayout>

FragmentActivity.java

  1. package com.zyf;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v4.app.FragmentManager;
  6. import android.support.v4.app.FragmentTransaction;
  7. import android.view.View;
  8. import android.widget.FrameLayout;
  9.  
  10. /**
  11. * fragment 使用实例
  12. * @see http://developer.android.com/training/basics/fragments/fragment-ui.html
  13. *
  14. * 3.0不需要继承FragmentActivity,因为3.0将比如getFragmentManager()方法已经加入到Activity中了。
  15. *
  16. * 3.0以前版本要通过继承FragmentActivity获得类似功能。
  17. */
  18. public class FragmentActivity extends android.support.v4.app.FragmentActivity {
  19.  
  20. FragmentManager fragmentManager;
  21. FrameLayout container;
  22. FragmentA a;
  23. FragmentB b;
  24.  
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28.  
  29. setContentView(R.layout.main);
  30.  
  31. // 得到框架布局控件
  32. container = (FrameLayout)findViewById(R.id.container);
  33.  
  34. // 返回与此活动相关的片段进行交互的FragmentManager
  35. fragmentManager = this.getSupportFragmentManager();
  36.  
  37. // 通过begintransaction方法获取一个事物处理实例。
  38. FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
  39.  
  40. a = new FragmentA();
  41. b = new FragmentB();
  42.  
  43. /** 在这期间可以使用 add(), remove(), 以及 replace(). 最终需要改变时执行 commit()即可 */
  44. mFragmentTransaction.add(R.id.container, a);
  45. mFragmentTransaction.commit();
  46. }
  47.  
  48. public void click(View view) {
  49. switch (view.getId()) {
  50. case R.id.a: // 按钮A
  51. show(a);
  52. break;
  53. case R.id.b: // 按钮B
  54. show(b);
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. private void show(Fragment frament) {
  61. FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
  62. mFragmentTransaction.replace(R.id.container, frament);
  63. mFragmentTransaction.addToBackStack(null);
  64. // mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//设置动画效果
  65. mFragmentTransaction.commit();
  66. }
  67. }

FragmentA.java

  1. package com.zyf;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10.  
  11. public class FragmentA extends Fragment {
  12.  
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. }
  17.  
  18. @Override
  19. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  20. Bundle savedInstanceState) {
  21.  
  22. View view = inflater.inflate(R.layout.fa, container, false);
  23.  
  24. // "AAAAAAAAAAAAA"按钮
  25. Button btn = (Button)view.findViewById(R.id.next);
  26. btn.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. Intent intent = new Intent(Intent.ACTION_VIEW);
  30. startActivity(intent);
  31. }
  32. });
  33. return view;
  34. }
  35.  
  36. // public void next(View view) {
  37. // Intent intent = new Intent(Intent.ACTION_VIEW);
  38. // startActivity(intent);
  39. // }
  40. }

FragmentB.java

  1. package com.zyf;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v4.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8.  
  9. public class FragmentB extends Fragment {
  10.  
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. // TODO Auto-generated method stub
  14. super.onCreate(savedInstanceState);
  15. }
  16.  
  17. @Override
  18. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  19. Bundle savedInstanceState) {
  20.  
  21. return inflater.inflate(R.layout.fb, container, false);
  22. }
  23.  
  24. // public void next(View view) {
  25. // Intent intent = new Intent(Intent.ACTION_VIEW);
  26. // startActivity(intent);
  27. // }
  28. }

Fragment切换页面的更多相关文章

  1. ViewPager+Fragment实现页面的切换

    新知识,新摘要: 效果图:framgent导入包都是v4包下,谨慎导入错误! 首先设置viewPager布局: <?xml version="1.0" encoding=&q ...

  2. Android - FragmentTabHost 与 Fragment 制作页面切换效果

    使用 FragmentTabHost 与 Fragment 制作页面切换效果 API 19 TabHost已经不建议使用了.用 FragmentTabHost 来代替TabHost.实际上 Fragm ...

  3. Android - TabHost 与 Fragment 制作页面切换效果

    Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...

  4. Fragment切换问题

    片断一: add hind @Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) { switch (check ...

  5. 对Fragment切换的优化

    在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment:然后总感觉切换的时候有些卡顿,原来的代码 /** * 切换页面,这里采用回调 * * @param f ...

  6. ViewPager+RadioGroup实现标题栏切换,Fragment切换

    1.说明: 在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用 ...

  7. fragment切换刷新 及下拉刷新

    此工程较BaiduLocationXMLFragmentDB相比:1.滑动fragment自动刷新该fragment2.下拉刷新fragment,上拉暂未实现 a.fragment切换刷新 1 . 由 ...

  8. ViewPager -- Fragment 切换卡顿 性能优化

    当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快速切换ViewPager,即 Fragment需要加载UI内容,而又频繁地切换Fragment, ...

  9. 实现Fragment 切换时不重新实例化

    以前实现Fragment的切换都是用replace方法实现 public void startFragmentAdd(Fragment fragment) { FragmentManager frag ...

随机推荐

  1. html+css+js实现科学计算器

    代码地址如下:http://www.demodashi.com/demo/13751.html 项目描述 纯html+css+js实现一个科学计算器,支持平方开方指数对数等基本函数,支持键盘输入,有简 ...

  2. oracle 查询 函数练习

    /*--以下代码是对emp表进行显示宽度设置col empno for 9999;col ename for a10;col job for a10;col mgr for 9999; col hir ...

  3. WPF SL 属性生成器

    在开发WPF 和SL应用的时候通用会用到MVVM模式,每次写到类属性的时候要不断的写属性更新时通知方法,写多了就嫌烦,就手写了个属性生成工具,在属性更新的时候添加了更新通知方法. 工具中支持自定义类对 ...

  4. vue.js使用之计算属性与方法返回的差别

    <!DOCTYPE html> <html> <head> <script src="https://unpkg.com/vue@2.4.2&quo ...

  5. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  6. 【原创】打造基于Dapper的数据访问层

    [原创]打造基于Dapper的数据访问层   前言 闲来无事,花几天功夫将之前项目里用到的一个数据访问层整理了出来.实现单个实体的增删改查,可执行存储过程,可输出返回参数,查询结果集可根据实际情况返回 ...

  7. 翻翻git之---实现QQ空间点赞部分实现的自己定义控件 EasyLikeArea

    转载请注明出处:王亟亟的大牛之路 昨天在家里弄鱼的事没上班,也就没写东西.决定今天早上补一篇,正好看到了 Easy like area in the circle of friends or QQ q ...

  8. WEEX快速入门

    WEEX快速入门 WEEX 是阿里推送的一款基于Node.js,轻量级的移动端跨平台动态性技术解决方案,用于构建原生的速度的跨平台APP. 1. 搭建WEEX环境 1.1 首先下载安装Node.js, ...

  9. windows编译tensorflow c++库

    1. 准备 windows 10系统.3.6GHz cpu.16G 内存 visual studio 2017 or 2015 下载安装git 下载安装cmake 下载安装swigwin 如果不需要p ...

  10. HTML5与XML的区别

    XHTML 是 XML 风格的 HTML 4.01. HTML5 是HTML的第五大版本,取代 HTML 4.01. XHTML是基于XML发布的HTML规范,旨在规范HTML的格式. 两者提出的目的 ...