Fragment_动态加载
1.新建Fragment的XML布局文件。
2.在activity.xml中添加需要加载Fragment。列如:
<?xml version="1.0" encoding="utf-8"?>
<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.example.administrator.fragment_01.activity.MainActivity"> <FrameLayout
android:id="@+id/fragment_id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" /> <LinearLayout
android:id="@+id/layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal"> <Button
android:id="@+id/activity_button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_1" /> <Button
android:id="@+id/activity_button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_2" /> </LinearLayout>
</LinearLayout>

3.创建MyFragment类继承Fragment类。
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.one_fragment, container,false);
return view;
}
}
4.创建待添加Fragment的实例。
5.获取FragmentManager。
6.开启一个事务。
7.向容器中添加或替换碎。
8.提交事务。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.activity_button_1);
Button button1 = (Button) findViewById(R.id.activity_button_2);
button.setOnClickListener(this);
button1.setOnClickListener(this);
//创建实例做参数穿入。
replaceFragment(new MyFragment());
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.activity_button_1:
replaceFragment(new MyFragment_2());
Toast.makeText(this, "点击了第一个", Toast.LENGTH_SHORT).show();
break;
case R.id.activity_button_2:
replaceFragment(new MyFragment());
Toast.makeText(this, "点击了第二个", Toast.LENGTH_SHORT).show();
break;
}
}
private void replaceFragment(Fragment fragment) {
//2.获取FragmentManager.
FragmentManager fragmentManager = getSupportFragmentManager();
//3.开启一个事务beginTransaction()
FragmentTransaction transaction = fragmentManager.beginTransaction();
//向容器中添加或替换碎片
transaction.replace(R.id.fragment_id, fragment);
//提交事务
transaction.commit();
}
}
Fragment_动态加载的更多相关文章
- js动态加载css和js
之前写了一个工具类点此链接里面含有这段代码,感觉用处挺多,特意提出来 var loadUtil = { /* * 方法说明:[动态加载js文件css文件] * 使用方法:loadUtil.loadjs ...
- geotrellis使用(二十三)动态加载时间序列数据
目录 前言 实现方法 总结 一.前言 今天要介绍的绝对是华丽的干货.比如我们从互联网上下载到了一系列(每天或者月平均等)的MODIS数据,我们怎么能够对比同一区域不同时间的数据情况,采用 ...
- Ext JS 如何动态加载JavaScript创建窗体
JavaScript不需要编译即可运行,这让JavaScript构建的应用程序可以变得很灵活.我们可以根据需要动态从服务器加载JavaScript脚本来创建和控制UI来与用户交互.下面结合Ext JS ...
- Ext动态加载Toolbar
在使用Ext的GridPanel时候,有时候需要面板不用重新加载而去更新Store或者Toolbar,Store的方法有很多,例如官方api给我们提供的Store.load(),Store.reLoa ...
- Android动态加载框架汇总
几种动态加载的比较 1.Tinker 用途:热修复 GitHub地址:https://github.com/Tencent/tinker/ 使用:http://www.jianshu.com/p/f6 ...
- 为不同分辨率单独做样式文件,在页面头部用js判断分辨率后动态加载定义好的样式文件
为不同分辨率单独做样式文件,在页面头部用js判断分辨率后动态加载定义好的样式文件.样式文件命名格式如:forms[_屏幕宽度].css,样式文件中只需重新定义文本框和下拉框的宽度即可. 在包含的头文件 ...
- html中的图像动态加载问题
首先要说明下文档加载完成是什么概念 一个页面http请求访问时,浏览器会将它的html文件内容请求到本地解析,从窗口打开时开始解析这个document,页面初始的html结构和里面的文字等内容加载完成 ...
- 非常郁闷的 .NET中程序集的动态加载
记载这篇文章的原因是我自己遇到了动态加载程序集的问题,而困扰了一天之久. 最终看到了这篇博客:http://www.cnblogs.com/brucebi/archive/2013/05/22/Ass ...
- Win8 Metro动态加载内容框架
制作背景 为了参加ImagineCup 2013 世界公民类比赛,我们设计制作了一个可动态扩展的幼教类App.这个App需要能动态加载内容,内容包括带动画可交互的电子书,动画,视频,游戏. 技术支持 ...
随机推荐
- Hadoop-2.2.0在Unbuntu ADM64中需要重新编译Native Lib
通过:cat /etc/issue 查看当前系统版本: Ubuntu 12.04.3 通过:uname -ar 查看更想起信息: Linux ubuntu-236 3.8.0-29-generic # ...
- PHP第一节课
基础语法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- HDU2866 Special Prime
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2866 题意:在区间[2,L]内,有多少个素数p,满足方程有解. 分析: 原方程变为: n^(b-1) ...
- tornado服务器运行django应用
在jumpserver项目中看到的 def main(): from django.core.wsgi import get_wsgi_application import tornado.wsgi ...
- CentOS7安装Kubernetes
CentOS7安装Kubernetes 安装Kubernetes时候需要一台机器作为管理机器,1台或者多台机器作为集群中的节点. 系统信息: Hosts: 请将IP地址换成自己环境的地址. cento ...
- 【Codeforces Round #519 by Botan Investments E】Train Hard, Win Easy
[链接] 我是链接,点我呀:) [题意] [题解] 设每个人做第一题.第二题的分数分别为x,y 我们先假设没有仇视关系. 即每两个人都能进行一次训练. 那么 对于第i个人. 考虑第j个人对它的贡献 如 ...
- Future和Callable的使用
应用场景 财务成本核算.可能会有多个耗时的步骤.如果顺序执行是非常慢的.再相互数据获取数据不依赖的情况下可以使用Future并行执行 public class FutureTest implement ...
- poj 1734 floyd求最小环,可得到环上的每个点
#include<stdio.h> #include<string.h> #define inf 100000000 #define N 110 #define min(a, ...
- myquant平台搭建及使用
1.主页 http://myquant.cn/ 点击“我要申请试用”,进入如下页面:http://myquant.cn/news/2015/03/25/try-gmsdk-v2.0/ 点击“试用注册” ...
- [React] Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3
getDerivedStateFromProps is lifecycle hook introduced with React 16.3 and intended as a replacement ...