在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++ delete 两次

    转载自:http://blog.csdn.net/jxluofeng/article/details/19766801 <问题>危险的代码: int* p=new int(1);   de ...

  2. openGL深度缓冲区问题

    http://zhidao.baidu.com/question/368299839.html&__bd_tkn__=6aa9196c746cd3357f1eec74aeb127b395029 ...

  3. Resin4 自定义端口

    1. Resin4配置文件发生了较大变化,分为: app-default.xml  web应用配置 cluster-default.xml  集群配置 health.xml -- 非pro版不支持 r ...

  4. Linux Oracle DB log 清理

    sid_list=`/bin/ps -ef |/bin/grep smon |/bin/grep -v grep | /bin/cut -f3 -d_` host_name=`hostname` cd ...

  5. AC日记——Valued Keys codeforces 801B

    801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...

  6. 洛谷——P1143 进制转换

    P1143 进制转换 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入输出格式 输入格式: 输入数据共有三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进 ...

  7. java中split任意数量的空白字符

    java程序中经常通过split截取字符串来取得其中的关键字.但是由于其中不同操作系统或者不同作者的习惯,经常会出现不同数量的空格,例如"   "(3个空格):或者出现制表符.Ta ...

  8. [POI2014]Supercomputer

    题目大意: 给定一个$n(n\le10^6)$个结点的有根树,从根结点开始染色.每次可以染和已染色结点相邻的任意$k$个结点.$q(q\le10^6)$组询问,每次给定$k$,问至少需要染几次? 思路 ...

  9. vs2012 ultimate 密钥

    Visual Studio Ultimate 2012 静态激活密钥,可以试一下. RBCXF-CVBGR-382MK-DFHJ4-C69G8

  10. setSystemUiVisibility() 与 getSystemUiVisibility() 显示隐藏状态栏

    Android  4.4  Camera 源码里面有一个操作界面的方法: /** * If {@param visible} is false, this hides the action bar a ...