Fragment切换页面
<?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切换页面的更多相关文章
- ViewPager+Fragment实现页面的切换
新知识,新摘要: 效果图:framgent导入包都是v4包下,谨慎导入错误! 首先设置viewPager布局: <?xml version="1.0" encoding=&q ...
- Android - FragmentTabHost 与 Fragment 制作页面切换效果
使用 FragmentTabHost 与 Fragment 制作页面切换效果 API 19 TabHost已经不建议使用了.用 FragmentTabHost 来代替TabHost.实际上 Fragm ...
- Android - TabHost 与 Fragment 制作页面切换效果
Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...
- Fragment切换问题
片断一: add hind @Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) { switch (check ...
- 对Fragment切换的优化
在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment:然后总感觉切换的时候有些卡顿,原来的代码 /** * 切换页面,这里采用回调 * * @param f ...
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
1.说明: 在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用 ...
- fragment切换刷新 及下拉刷新
此工程较BaiduLocationXMLFragmentDB相比:1.滑动fragment自动刷新该fragment2.下拉刷新fragment,上拉暂未实现 a.fragment切换刷新 1 . 由 ...
- ViewPager -- Fragment 切换卡顿 性能优化
当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快速切换ViewPager,即 Fragment需要加载UI内容,而又频繁地切换Fragment, ...
- 实现Fragment 切换时不重新实例化
以前实现Fragment的切换都是用replace方法实现 public void startFragmentAdd(Fragment fragment) { FragmentManager frag ...
随机推荐
- 织梦程序中plus文件作用介绍及安全设置
官方网站下载了Dedecms安装包以后,解压出来,有一个uploads文件,这里面的文件夹才是网站的安装文件,里面文件很多,今天无忧小编就主要介绍下plus文件夹里面的各个功能模块,如果你只是做一个宣 ...
- 在windows 2008 R2上安装sharepoint 2013时遇到提示必须安装 .netframeword4.5的处理办法
近日,有个客户需要个测试环境,需要搭建一个sps2013的测试服务器 但基于服务器软件授权的考虑,让使用windows2008 作为系统服务器 但一安装就碰到个提示,要求先安装framework4.5 ...
- 前端资源管理工具sourcetree
https://www.sourcetreeapp.com/
- crc32 根据字符串获取校验值
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text; n ...
- Apache 虚拟目录和默认首页的设置
Apache虚拟目录和默认首页的设置,用apache服务器的朋友必须要懂的. 虚拟目录 1.找到"conf/httpd.conf" 文件 2.在节点:<IfModule al ...
- BootCamp支持软件6
最新版本的 Boot Camp 6 苹果驱动支持的机型列表 苹果官方已经公布了 BootCamp 6 驱动支持的机型列表了,基本上 2012 年后的 Macbook / Pro / Air / iMa ...
- Windows调试神器:WinDBG
Q:WinDBG的Watch窗口中我想要查看长字符串,但是后面的内容都被省略为...了怎么办? A:如图,双击你要查看的内容,出现光标后,移动光标即可查看后面被省略的内容 Q:WinDBG如何给程序设 ...
- 安装php WampServer之后,运行的时候报错“phpMyAdmin - 错误 缺少 mysqli 扩展。请检查 PHP 配置。”
今天在安装了WampServer2.1a-x32之后,点击WampServer的图标启动WampServer,在电脑右下角就可以看到WampServer启动之后的图标,然后点击“Start All S ...
- GCD 莫比乌斯反演 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对.
/** 题目:GCD 链接:https://vjudge.net/contest/178455#problem/E 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对( ...
- 8237dma的四种传送方式简介
8237A有四种工作方式:单字节传送.数据块传送.请求传送和多片级联. (1)单字节传送(single mode) 单字节传送方式是每次DMA传送时,仅传送一个字节.传送一个字节之后,当前字节计数器减 ...