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 ...
随机推荐
- 关于org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor的队列
今天查看源码发现spring的线程池是支持队列的: 并且队列支持的上限相当大: 当线程池的达到最大线程时,默认会把任务放在队列(内存)中,所以我们可以放心用这个东西来写日志了
- T-sql for xml path使用
用法: FOR XML PATH 方法是用于将查询结果集以XML形式展示 sql: p.ContactTypeID,p.ModifiedDate,p.Name from [Person].[Conta ...
- js Array.prototype.join.call(arguments,",") 理解
prototype 属性使您有能力向对象添加属性和方法. join() 方法:把数组的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. call() 方法可以用来代替另一个对象调用一个方法. A ...
- Linux yum操作无效的解决方法
1.没网,试着:ping www.baidu.com 如果显示没有连接的话,就说明没网,也就无法使用yum 命令. 2.ping通了的话,还是是用不了yum命令,说明是yum镜像没有了,那么就得下载一 ...
- Groovy小结:java调用Groovy方法并传递参数
Groovy小结:java调用Groovy方法并传递参数 @(JAVA总结) 1. 场景描述 在网上查了资料发现,java有三种方式调用groovy脚本.但是真正在实际的服务器环境中,嵌入groovy ...
- GitHub的Fork 是什么意思
现在有这样一种情形: 有一个叫做Joe的程序猿写了一个游戏程序,而你可能要去改进它.并且Joe将他的代码放在了GitHub仓库上. 下面是你要做的事情 fork并且更新GitHub仓库的图表演示 Fo ...
- Redis 5种数据类型,2种特殊数据处理策略
5种数据类型 String [html] view plaincopy 1.String 经常使用命令: 除了get.set.incr.decr mget等操作外,Redis还提供了下面一些操 ...
- offsetof宏的实现
1.c语言的结构体中,因为字节对齐的问题,导致成员地址并不能根据类型的大小进行计算.例如: struct test { char ch; int a; } printf("test的大小=% ...
- vue2项目 :在hosts里面配置了装逼的模式。设置应用在127.0.0.1:80端口访问; 并将127.0.0.1指向www.yours.com ;问题“ Invalid Host header”
转自博客:https://www.cnblogs.com/cynthia-wuqian/p/8575401.html 1.MAC设置应用在127.0.0.1:80端口访问: config/index. ...
- android - px(像素)、dpi(像素密度)、dip(密度无关像素)之间的关系
使用ImageView会遇到的问题 在Android应用中,都少不了图片的显示,ImageView,轮播图,ViewPager等等,很多都是来显示图片的,很多时候,我们都希望图片能够在宽度上填充父窗体 ...