Fragment,在平板应用中较为参见,把视图分为两个甚至多个模块。

一,一个简单的fragment

1.创建两个局部文件,用于等待被调用

(1)left_fragment

(2)right_fragment

2.分别创建两个继承于fragment类的类

(1)leftFragment(加载xml文件)

public class LeftleftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment,container,false);//动态加载xml
return view;
}
}

(2)rightFragment

我是代码(同上);

3.在activity所调用的xml中添加<fragment>控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <fragment
android:id="@+id/left_fragment"
android:name="com.cenzhongman.myapplication.fragment.LeftleftFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/> <fragment
android:id="@+id/right_fragment"
android:name="com.cenzhongman.myapplication.fragment.RightFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>

二,动态切换fragment

4.在原基础之上创建AnotherRightFragment(指向要调用的layout)

我是代码(同上,较少重复加载)

5.在MainActivity所调用xml中更改布局方式

用两FrameLayout(强制左上角对齐的布局),来存放fragment,动态更新时候将会更换FrameLayout中的内容,也就是旗下的FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <FrameLayout
android:id="@+id/left_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"> <fragment
android:name="com.cenzhongman.myapplication.fragment.LeftleftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment" />
</FrameLayout> <FrameLayout
android:id="@+id/right_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"> <fragment
android:name="com.cenzhongman.myapplication.fragment.RightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </FrameLayout>
</LinearLayout>

6.在MainActivity中更换fragment

public void onClick(View v) {
AnotherRightFragment anotherRightFragment = new AnotherRightFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_fragment, anotherRightFragment);
transaction.commit();//必须调用commit()方法,否则显示不出来
}

1. 创建待添加的碎片实例。
        2. 获取到 FragmentManager,在活动中可以直接调用 getFragmentManager()方法得到。
        3. 开启一个事务,通过调用 beginTransaction()方法开启。
        4. 向容器内加入碎片,一般使用 replace()方法实现,需要传入容器的 id 和待添加的碎
            片实例。
        5. 提交事务,调用 commit()方法来完成。

三,把fragment添加到返回栈

transaction.addToBackStack(null);//接收一个名字用于描述返回栈的状态,一般传入null即可

用fragment来做导航

1.在MainActivity的xml留出用于装fragment的容器

2.写好Fragment的xml描述文件

3.写四个Fragment的实现类,继承于Fragment

public class IdeaFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_idea,container,false);
return view;
}
}

4.MainActivity应继承于FragmentActivity,并创建Fragment对象

//定义fragment对象,基于v4包
private Fragment study_fragment;
private Fragment app_fragment;
private Fragment idea_fragment;
private Fragment personal_fragment; //定义fragmentManager对象
FragmentManager mFragmentManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFragmentManager = getSupportFragmentManager();//基于v4包
initView(); }

5.得到FragmentTransaction对象,并根据情况选择添加或者显示fragment

!!!一定要commit()!!!

FragmentTransaction transaction = mFragmentManager.beginTransaction();

idea_Button.setImageResource(R.drawable.idea_blue);
idea_TextView.setTextColor(getResources().getColor(R.color.sky_blue));
if (idea_fragment == null) {
idea_fragment = new IdeaFragment();
transaction.add(R.id.content, idea_fragment);
transaction.commit();
} else {
transaction.show(idea_fragment);
transaction.commit();
}

参考资料:Fragment之底部导航栏的实现

Fragment碎片的创建和动态更新的更多相关文章

  1. 动态更新Toolbar Menu以及Menu中同时显示文字和图标

    动态更新Toolbar Menu以及Menu中同时显示文字和图标 我们经常会有这样的需求,在切换Fragment或者点击某个按钮后动态更新Toolbar上Menu项.但是onCreateOptions ...

  2. 14 Fragment 碎片总结

    Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...

  3. 在Android中实现service动态更新UI界面

    之前曾介绍过Android的UI设计与后台线程交互,据Android API的介绍,service一般是在后台运行的,没有界面的.那么如何实现service动态更新UI界面呢?案例:通过service ...

  4. Android中动态更新ListView(转)

    在使用ListView时,会遇到当ListView列表滑动到最底端时,添加新的列表项的问题,本文通过代码演示如何动态的添加新的列表项到ListView中.实现步骤:调用ListView的setOnSc ...

  5. iOS 利用 Framework 进行动态更新

    http://nixwang.com/2015/11/09/ios-dynamic-update/ 前言 目前 iOS 上的动态更新方案主要有以下 4 种: HTML 5 lua(wax)hotpat ...

  6. highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度

    highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度 作者:highcharts | 时间:2014-6-11 14:07:05 | [小  大] | ...

  7. AsyncTask用法解析-下载文件动态更新进度条

    1. 泛型 AysncTask<Params, Progress, Result> Params:启动任务时传入的参数,通过调用asyncTask.execute(param)方法传入. ...

  8. 仿百度壁纸客户端(五)——实现搜索动画GestureDetector手势识别,动态更新搜索关键字

    仿百度壁纸客户端(五)--实现搜索动画GestureDetector手势识别,动态更新搜索关键字 百度壁纸系列 仿百度壁纸客户端(一)--主框架搭建,自定义Tab + ViewPager + Frag ...

  9. antd Select进阶功能 动态更新、函数防抖

    一.动态更新Options Antd Select自带的搜索功能很多时候需要结合后端的接口,输入一个关键字的时候会自动更新选择器的选项. 下面列一些注意点 基础实现 选择器选项必须和每次更新的数据挂钩 ...

随机推荐

  1. Linux_Shell_脚本参数接收键盘输入

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/bin/bash     #提示"请输入姓名"并等待30秒,把用户的输入保存入变量name ...

  2. Elasticsearch静态集群配置

    这两天需要将ELK中的单节点运行的ES扩展为双节点,查询了下集群配置,百度搜索结果还是一如既往的坑,基本都是各种转帖,以下记录配置静态集群的步骤: * * * <pre><code& ...

  3. 安装Win7提示Windows无法安装到磁盘怎么办

    Windows之家(www.windowszj.com):在安装Win7系统的过程中,由于每台电脑的状态不一样,比如硬件配置原因,或者是硬盘格式.硬盘状态等问题,会使得每台电脑在安装过程中都会有些不一 ...

  4. Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现

    笔者最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word,excel,txt)只能预览不允许下载.笔者想到了百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash ...

  5. PHP热身

    0.环境说明 win10 xampp phpstorm xampp带了apache和mysql以及phpmyadmin 1.配置mysql:修改默认编码 关mysql xampp/mysql/bin/ ...

  6. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  7. 洛谷P1565 牛宫

    题目描述 AP 神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M 的矩形空地. 空地中每个格子都有自己的海拔高度.AP 想让他的宫殿的平均海拔在海平面之上(假设 海平面的高度是0,平均数都会 ...

  8. dedecms /member/reg_new.php SQL Injection Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Dedecms会员中心注入漏洞 2. 漏洞触发条件 http://127 ...

  9. CF 702B Powers of Two(暴力)

    题目链接: 传送门 Devu and Partitioning of the Array time limit per test:3 second     memory limit per test: ...

  10. jboss性能优化

    jboss     linux jboss 部署时优化设置: 在/conf/web.xml中通过参数指定: <session-config>          <session-ti ...