动态添加Fragment
在Fragment简单用法的基础上做修改
一.新建:another_right_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff">
<TextView
android:text="另一个raight fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_weight="1" />
</LinearLayout>
二.新建AnotherRightFragment类:
public class AnotherRightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_right_fragment, container, false);
return view;
}
}
三.修改activity_main.xml:
<?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">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.guo.fragment.LeftFragment"
android:layout_weight="1"
android:id="@+id/left_fragment" />
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.guo.fragment.RightFragment"
android:layout_weight="1"
android:id="@+id/right_fragment" />
</LinearLayout>
四.修改left_fragment.xml
给按钮增加android:onClick="myClick"
五.修改MainActivity,增加click方法,
protected void myClick(View view){
if( view.getId() == R.id.btn ){
AnotherRightFragment anotherRightFragment = new AnotherRightFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.right_fragment, anotherRightFragment);
fragmentTransaction.addToBackStack(null); // 按下返回键,返回上一个Fragment;不加改行,则直接退出应用
fragmentTransaction.commit();
}
}
可以看到,首先我们给左侧碎片中的按钮注册了一个点击事件,然后将动态添加碎片的
逻辑都放在了点击事件里进行。结合代码可以看出,动态添加碎片主要分为5 步。
1. 创建待添加的碎片实例。
2. 获取到FragmentManager,在活动中可以直接调用getFragmentManager()
方法得到。
3. 开启一个事务,通过调用beginTransaction()方法开启。
4. 向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id 和待添加的
碎片实例。
5. 提交事务,调用commit()方法来完成。
动态添加Fragment的更多相关文章
- 使用Fragment 实现动态UI 和 动态添加Fragment
首先写好每个Fragment: 1.在第一个Fragment写一个按钮,使其加载下一个Fragment 布局: <LinearLayout xmlns:android="http:// ...
- Android Fragment用法详解(2)--动态添加Fragment
在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...
- Activity动态添加Fragment时遇到的问题
1.Activity动态调用代码 TitleFragement a = new TitleFragement(); getFragmentManager().beginTransacti ...
- Android动态添加Fragment
Android动态添加Fragment 效果图如下: 项目结构图如下: Fragment1: package com.demo.dongtaifragment; import android.app. ...
- 深入分析动态管理Fragment
一.Fragment与Activity的关系 要理解动态管理Fragment首先要理解一下Fragment与Activity的关系 Fragment可以理解为将Activity划分为若干个片段,Fra ...
- 安卓开发_浅谈Fragment之事务添加Fragment对象
我们都知道给一个activity动态添加fragment的时候 有下面几种添加方式 看一下布局文件 <LinearLayout xmlns:android="http://schema ...
- Android Fragment动态添加 FragmentTransaction FragmentManager
Fragment常用的三个类:android.app.Fragment 主要用于定义Fragmentandroid.app.FragmentManager 主要用于在Activity中操作Fragme ...
- 用Javascript动态添加删除HTML元素实例 (转载)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android 动态创建Fragment
Fragment是activity的界面中的一部分或一种行为.可以把多个Fragment组合到一个activity中来创建一个多界面并且可以在多个activity中重用一个Fragment.可以把Fr ...
随机推荐
- YV12 NV12区别
用videoCapture和IAMStreamConfig拿到的支持的格式列表.发现支持2中图像格式,YV12和NV12.具体是怎么样的内存分布不知道.查了些文档.自己修改了几个图.看出了点端倪 YV ...
- SqliteDeveloper 破解
方法1: 注册表删除 HKEY_CURRENT_USER\SharpPlus\SqliteDev 下的 StartDate 方法2: 安装完成SQLite Developer后,先不要打开软件,下面 ...
- QML与Qt C++ 交互机制探讨与总结(转)
原文转自 https://www.cnblogs.com/aoldman/p/4103510.html 介绍 QML和 C++对象可以通过,signals,slots和 属性修改进行交互.对于一个C+ ...
- 【程序打包工具 Inno Setup】CreateProcess 失败:代码 740(Inno Setup打包的程序提升为管理员权限)
原文参考 https://www.cnblogs.com/SnailProgramer/p/4243666.html http://blog.csdn.net/x356982611/article/d ...
- MFC 菜单操作
在CMainFrame中OnCreate函数中添加下列:(注意在return(0)前添加) 在文件,新建前打钩 法1: GetMenu()->GetSubMenu(0)->CheckMen ...
- 有个人想上一个n级的台阶,每次只能迈1级或者迈2级台阶,问:这个人有多少种方法可以把台阶走完?
有个人想上一个n级的台阶,每次只能迈1级或者迈2级台阶,问:这个人有多少种方法可以把台阶走完? 相关问题: (1)有个人想上一个n级的台阶,每次只能迈1级或者迈2级台阶,问:这个人有多少种方法可以把台 ...
- Express定制参数解析错误响应值
Nodejs的Express框架本身所提供的东西并没有其它框架那么多.其中的一个问题就是对于请求数据的解析. express中的请求对象并没有未经过解析的请求体,几乎所有的请求体都要经过类似于body ...
- ZOJ 3820:Building Fire Stations(树的直径 Grade C)
题意: n个点的树,边长全为1,求找出两个点,使得树上离这两个点距离最远的那个点,到这两个点(中某个点就行)的距离最小. 思路: 求树直径,找中点,删除中间那条边(如果直径上点数为奇数,则删任何一侧都 ...
- MVC如何在路由器(RouteConfig)定义后缀.html
一.配置文件web.config添加一下设置 <system.webServer> <modules runAllManagedModulesForAllRequests=" ...
- android launchmode singleinstance问题
问题描述 最近测试关于launchmode的四种方式 默认模式 top singletask 都已经了解了 唯独这个instance模式 我的问题是 我们只作2个activity的假设A和B,其中A为 ...