在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的更多相关文章

  1. 使用Fragment 实现动态UI 和 动态添加Fragment

    首先写好每个Fragment: 1.在第一个Fragment写一个按钮,使其加载下一个Fragment 布局: <LinearLayout xmlns:android="http:// ...

  2. Android Fragment用法详解(2)--动态添加Fragment

    在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...

  3. Activity动态添加Fragment时遇到的问题

    1.Activity动态调用代码 TitleFragement a = new TitleFragement();        getFragmentManager().beginTransacti ...

  4. Android动态添加Fragment

    Android动态添加Fragment 效果图如下: 项目结构图如下: Fragment1: package com.demo.dongtaifragment; import android.app. ...

  5. 深入分析动态管理Fragment

    一.Fragment与Activity的关系 要理解动态管理Fragment首先要理解一下Fragment与Activity的关系 Fragment可以理解为将Activity划分为若干个片段,Fra ...

  6. 安卓开发_浅谈Fragment之事务添加Fragment对象

    我们都知道给一个activity动态添加fragment的时候 有下面几种添加方式 看一下布局文件 <LinearLayout xmlns:android="http://schema ...

  7. Android Fragment动态添加 FragmentTransaction FragmentManager

    Fragment常用的三个类:android.app.Fragment 主要用于定义Fragmentandroid.app.FragmentManager 主要用于在Activity中操作Fragme ...

  8. 用Javascript动态添加删除HTML元素实例 (转载)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Android 动态创建Fragment

    Fragment是activity的界面中的一部分或一种行为.可以把多个Fragment组合到一个activity中来创建一个多界面并且可以在多个activity中重用一个Fragment.可以把Fr ...

随机推荐

  1. C# 操作Excel,使用EPPlus

    EPPlus下载地址:http://www.codeplex.com/EPPlus 引用命名空间: using OfficeOpenXml; using OfficeOpenXml.Table; us ...

  2. 杭电oj2028、2034、2035、2041、2043-2046

    2028  Lowest Common Multiple Plus #include<stdio.h> int gcd(int a,int b){ int temp,temp1; if(a ...

  3. AC日记——[Wc2008]游览计划 bzoj 2595

    2595 思路: 状压DP+spfa转移+dfs输出路径: 或者,斯坦纳树算法模板: 来,上代码: #include <queue> #include <cstdio> #in ...

  4. Future使用场景与分析

    前面分享了CountDownLatch的用法,但是由于分享过程中,发现有些朋友,问我Future与CountDownLatch的有什么区别? 答案:只是concurrent包下的并发帮助工具类,两者并 ...

  5. Python的并发并行[1] -> 线程[0] -> threading 模块

    threading模块 / threading Module 1 常量 / Constants Pass 2 函数 / Function 2.1 setprofile()函数 函数调用: thread ...

  6. Python的并发并行[3] -> 进程[0] -> subprocess 模块

    subprocess 模块 0 模块描述 / Module Description From subprocess module: """Subprocesses wit ...

  7. Codeforces 1010D Mars rover

    题目大意:对于一个不完全二分图,根节点为1,叶节点值为0或1,非叶节点包含一个操作(and,or,xor,not),求改变各个叶节点的值时(即0改为1,1改为0),根节点的值是多少 解法:遍历图求各节 ...

  8. js部分的总结

    一.词法结构 1.区分大小写 2.注意//单行/*多行注释* 3.字面量(直接量 literal) 12 //数字 5.8 //小数 “hello” ‘hello’ true /js/gi  //正则 ...

  9. Java this关键字详解

    this 关键字用来表示当前对象本身,或当前类的一个实例,通过 this 可以调用本对象的所有方法和属性.例如: public class Demo{ public int x = 10; publi ...

  10. 每天一个liunx命令2之rz和sz命令

    1安装命令工具包     yum install lrzsz   2sz命令发送文件到本地(send): sz filename 3rz命令本地上传文件到服务器(receive): rz 执行该命令后 ...