<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <FrameLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/a"
android:onClick="click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Fragment A"/>
<Button
android:id="@+id/b"
android:onClick="click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Fragment B"/>
</LinearLayout> </LinearLayout>

FragmentActivity.java

package com.zyf;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout; /**
* fragment 使用实例
* @see http://developer.android.com/training/basics/fragments/fragment-ui.html
*
* 3.0不需要继承FragmentActivity,因为3.0将比如getFragmentManager()方法已经加入到Activity中了。
*
* 3.0以前版本要通过继承FragmentActivity获得类似功能。
*/
public class FragmentActivity extends android.support.v4.app.FragmentActivity { FragmentManager fragmentManager;
FrameLayout container;
FragmentA a;
FragmentB b; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main); // 得到框架布局控件
container = (FrameLayout)findViewById(R.id.container); // 返回与此活动相关的片段进行交互的FragmentManager
fragmentManager = this.getSupportFragmentManager(); // 通过begintransaction方法获取一个事物处理实例。
FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction(); a = new FragmentA();
b = new FragmentB(); /** 在这期间可以使用 add(), remove(), 以及 replace(). 最终需要改变时执行 commit()即可 */
mFragmentTransaction.add(R.id.container, a);
mFragmentTransaction.commit();
} public void click(View view) {
switch (view.getId()) {
case R.id.a: // 按钮A
show(a);
break;
case R.id.b: // 按钮B
show(b);
break;
default:
break;
}
}
private void show(Fragment frament) {
FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
mFragmentTransaction.replace(R.id.container, frament);
mFragmentTransaction.addToBackStack(null);
// mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//设置动画效果
mFragmentTransaction.commit();
}
}

FragmentA.java

package com.zyf;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button; public class FragmentA extends Fragment { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fa, container, false); // "AAAAAAAAAAAAA"按钮
Button btn = (Button)view.findViewById(R.id.next);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
startActivity(intent);
}
});
return view;
} // public void next(View view) {
// Intent intent = new Intent(Intent.ACTION_VIEW);
// startActivity(intent);
// }
}

FragmentB.java

package com.zyf;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FragmentB extends Fragment { @Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.fb, container, false);
} // public void next(View view) {
// Intent intent = new Intent(Intent.ACTION_VIEW);
// startActivity(intent);
// }
}

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. 04-spring-控制反转

    使用myeclipse开发spring一个Demo. 第一步:新建一个web project. 第二步:安装spring开发的支持包. 安装后多了这几个东西 3,定义一个操作接口: package c ...

  2. 无需超级用户mpi多机运行

    在之前的一篇博文中(Linux下mpi环境配置与运行步骤(Ubuntu为例) ),有讨论过怎样使用MPI在两个不同的机器上运行程序,在那篇博文中使用了超级用户权限.不幸的是.有些情况下,我们不能拥有操 ...

  3. struts.xml 文件添加DTD文件

    在编辑struts.xml 文件时,“alt + /”无提示信息,需要在myeclipse 中添加消息头中的文件,步骤如下: 1. 选中该段复制 2. Preferences——>XML Cat ...

  4. Python-装饰器进阶

    基本概念 具体概念请先看之前的文章 理解装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理, Web权限校验, Cache等. 很有名的例子,就 ...

  5. rsync工具介绍

    rsync工具介绍 http://man.linuxde.net/rsync rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的“rsync算法” ...

  6. laydate 和 Vue 奇怪的清空问题

    laydate的input,会自动被清空,当别的input修改的时候.改成这样既可解决 <td><input type="text" id="retur ...

  7. C#颜色 Color.FromArgb ColorTranslator 16进制

    //方法1: //引用命名空间 using System.Drawing; 16进制颜色代码转Color类型:ColorTranslator.FromHtml(color); Color类型转16进制 ...

  8. c#各类型转byte[]或转回

    var tmp = BitConverter.ToInt32(new byte[]{...}); var bytes = BitConverter.GetBytes(tmp); 而String转byt ...

  9. 多线程-Thread,Runnable,Callable,Future,RunnableFuture,FutureTask

    类图: 先看各自的源码: public interface Runnable { public abstract void run(); } public class Thread implement ...

  10. Atitit.ati dwr的原理and设计 attilax 总结 java php 版本

    Atitit.ati dwr的原理and设计 attilax 总结 java php 版本 1. dwr的优点相对于ajax来说..1 2. DWR工作原理1 3. Dwr的架构2 4. 自定义dwr ...