Android学习之动态调用碎片
一、碎片可以在程序运行时动态添加到活动当中。我们可以根据具体情况动态地添加碎片,使得界面定制更加多样化,下面将用个例子来介绍动态调用碎片。
先附上下面例子运行后的效果,效果如下:
点击左边按钮后动态改变右边的布局,点击按钮后效果如下:
二、首先,新建一个android项目,项目名为FragmentTest2,主要项目结构如下图所示:
1、首先,先新建三个布局文件,分别为fragment1.xml,fragment2.xml、fragment3.xml,代码分别如下:
fragment1.xml:
<?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"android:orientation="vertical" ><Button
android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="Hello Android" /></LinearLayout>
fragment2.xml:
<?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"android:orientation="vertical"android:background="@drawable/logo" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="Hello world"android:textSize="20sp"/></LinearLayout>
fragment3.xml:
<?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"android:orientation="vertical"android:background="#FFCCFF" ><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher"android:layout_gravity="center"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="Hello Android"android:textSize="20sp"/></LinearLayout>
其中fragment1.xml代表左边的碎片布局,fragment2.xml代表右边的布局,fragment3.xml代表点击左边碎片布局的按钮后,右边碎片显示的布局,大家看下代码和刚才的效果图就知道了。
2、新建三个类,分别为Fragment1,Fragment2,Fragment3类,这三个类都继承于Fragment类,代码分别如下:
Fragment1:
package com.example.fragmenttest2;import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class Fragment1 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(R.layout.fragment1, container);return view;}}
Fragment2:
package com.example.fragmenttest2;import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class Fragment2 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment2, container);return view;}}
Fragment3:
package com.example.fragmenttest2;import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class Fragment3 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(R.layout.fragment3, container,false);return view;}}
其中这三个类都是加载相应的布局文件。
注意:大家发现了没,第三个类Fragment3的代码有点不同,在实例化View的时候,使用LayoutInflater的inflater()方法里多加了个布尔型的参数,因为此项目的功能会使原始右边碎片的布局改变,即view改变,所以必须要在改变后的那个碎片类加载布局时,实例化View对象时要多一个false参数,对于此项目改变后的碎片为Fragment3,所以就在此加个参数,否则会报错。其实我们可以把上面三个类实例化View对象时多一个参数,也不影响的。
3、接着需改变默认的activity_main布局文件,代码如下:
<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:id="@+id/fragment1"android:name="com.example.fragmenttest2.Fragment1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><FrameLayout
android:id="@+id/frameLayout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" ><fragment android:id="@+id/fragment2"android:name="com.example.fragmenttest2.Fragment2"android:layout_width="match_parent"android:layout_height="match_parent"/></FrameLayout></LinearLayout>
上述代码放入了一个左侧碎片,把右侧碎片放入了FrameLayout布局中,现在放置的右侧碎片为fragment2,所以接下来就需要动态调用碎片,将右侧的碎片改为fragment3,即得在代码中替换FrameLayout里的碎片。
4、接下来修改MianActivity类,代码如下:
package com.example.fragmenttest2;import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch (arg0.getId()) {case R.id.button:Fragment3 fragment3 = new Fragment3();// 实例化Fragment3对象FragmentManager fragmentManager = getFragmentManager();// 获得FragmentManager对象FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// 开始事务fragmentTransaction.replace(R.id.frameLayout, fragment3);//添加fragment3到容器fragmentTransaction.addToBackStack(null);//添加返回栈fragmentTransaction.commit();// 提交事务break;default:break;}}}
其中这个Activity实现了OnClickListener接口,然后给左侧碎片的按钮添加了单击事件,点击按钮后触发onClick()方法,其中动态调用碎片的逻辑都在点击事件里进行。
结合上述代码可以看出,动态添加碎片主要分五步:
第一步:创建待添加的碎片实例。
第二步:获取到FragmentManager,在一个活动中可直接使用getFragmentManage方法得到。
第三步:开启一个事务,通过调用beginTransaction()方法开启。
第四步:向容器中加入碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。
第五步,提交事务,调用commit()方法来完成。
其中第四步向容器里加入碎片,传入容器的id为放置碎片的FrameLayout。
注意:其中我在上述代码中多添加了一句代码,代码如下:
fragmentTransaction.addToBackStack(null);//添加返回栈
第一点:此代码的功能为在碎片中模拟返回栈,这样不会使我们动态调用碎片后,点击物理键盘的返回按钮,会直接退出,而是动态调用碎片后,点击返回键,返回的是上一个碎片,模仿类似于返回栈的效果。
第二点:必须在事务提交之前调用了 FragmentTransaction 的 addToBackStack()方法,它可以接收一个名字用于描述返回栈的状态,一般传入 null 即可。
5.部署此程序到平板模拟器中,会达到上图提到的效果,点击按钮右侧碎片改变,点击返回物理键盘,返回到上一个碎片,在点击返回键,便退出此应用。
三、以上内容仅供大家学习参考,谢谢!
上述源码下载:http://download.csdn.net/download/u012561176/9259285
Android学习之动态调用碎片相关推荐
- android image对象改变,【Android学习】动态放大缩小ImageView里的图片,运用Matrix对象来....
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 /* 设置图片缩小的比例 */ double scale=0.8; /* 计算出这次要缩小的比例 */ scaleWidth=(float) (scale ...
- Android学习——Fragment动态加载
动态加载原理 利用FragmentManager来添加一套Fragment事务,最后通过commit提交该事务来执行对Fragment的相关操作. FragmentManager fragmentma ...
- android 字体跳动,【Android学习】动态文字闪动效果
在学安卓之前有在其他网站上看到闪动文字的自定义控件,感觉很炫酷,高大上. 直到我看了<Android群英传>,这本书真不错,强烈推荐. 新手进阶很有帮助. 效果: 书中原文: 利用Pain ...
- Android学习之碎片的生命周期
一.碎片的状态: 1.运行状态:当一个碎片是可见的,并且它所关联的活动正处于运行状态时,该碎片也处于运行状态. 2.暂停状态:当一个活动进入暂停状态时(由于另一个未占满屏幕的活动被添加到了栈顶),与它 ...
- Android 第十七课 碎片的简单用法及动态添加碎片
Fragment(碎片)是一种可以嵌入在活动当中的UI片段,它可以让程序更加合理和充分的利用大屏幕的空间.碎片和活动太像了,同样都包含布局,都有自己的声明周期,可以将碎片理解为一种迷你型的活动. 新建 ...
- Android学习之碎片与活动之间的通信
一.碎片虽然是嵌入在活动中显示的,但是碎片和活动都是各自存在不同的类当中的,并没有什么明显的方式来直接进行通信的.那么如果要在活动中调用碎片里的方法,在碎片里调用活动的方法,一个碎片调用另一碎片的方法 ...
- Android学习笔记(4)——探究碎片
第四章 手机平板要兼顾--探究碎片 第四章 4.1 碎片是什么 4.2 碎片的使用方式 4.2.1 碎片的简单实用 4.2.2 动态添加碎片 4.2.3 在碎片中模拟回收栈 4.2.4 碎片和活动之间 ...
- Android学习——碎片(fragment)
碎片 1.碎片是什么 2.碎片的使用方式 2.1静态加载Fragment 2.2动态添加碎片 2.3在碎片中返回栈 2.4Fragment管理与Frangment事务 2.5Fragment与Acti ...
- Android开发之动态库调用
发信人: yangAlbert (蓝), 信区: Android 标 题: Android开发之动态库调用 发信站: 武汉白云黄鹤站 (2011年02月20日23:20:51 星期天) 1.编写并生 ...
最新文章
- Android studio 启动自学模式
- html5遍历集合数据,集合框架系列教材 (五)- ArrayList - 遍历ArrayList的三种方法...
- 太牛了!这所211大学,又有95后硕士生一作发Nature!
- Dubbo 迈出云原生重要一步 - 应用级服务发现解析
- 定制安装centos6.4系统 上汽集团线上环境
- Java反射机制概念及应用场景
- 2021年的高考大约多久可以查询成绩,2021高考完什么时候可以查分数 查成绩的时间...
- 首发骁龙665 小米CC9e 4+128G版到手价1199元
- 2016.08.15
- 集成Atlas到现有的网站项目中--决定弃用Ajaxpro转用Atlas了
- Android 代码管理技巧
- mc服务器地图无限大吗,我的世界:4个小秘密,没想到啊,地图的范围这么大!...
- sonarqube配置全指南,集成阿里巴巴p3c规范
- vue3.0脚手架的搭建
- 高数 | 精通中值定理 解题套路汇总
- Java程序出现不正常情况
- 怎么教你如何查看电脑的蓝牙版本【解决方案】
- [译] APT分析报告:01.Linux系统下针对性的APT攻击概述
- 螺旋传动设计系统lisp_螺旋传动设计
- html中国家的下拉列表,jQuery Select下拉列表国家选择插件