Android学习笔记_46_fragment的简单应用
Fragments 诞生初衷
http://www.cnblogs.com/TerryBlog/archive/2012/02/17/2355753.html
自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。其上的是为了解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件,而多出来的空间存放UI使其会产生更多的交互,从而诞生了fragments 。fragments 的设计不需要你来亲自管理view hierarchy 的复杂变化,通过将Activity 的布局分散到frament 中,可以在运行时修改activity 的外观,并且由activity 管理的back stack 中保存些变化。
Fragments 设计理念
在设计应用时特别是Android 应用 ,有众多的分辨率要去适应,而fragments 可以让你在屏幕不同的屏幕上动态管理UI。例如:通讯应用程序(QQ),用户列表可以在左边,消息窗口在右边的设计。而在手机屏幕用户列表填充屏幕当点击某一用户时,则弹出对话窗口的设计,如下图:
Fragments的生命周期
每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件。 对应生命周期可参考下图:
其中大多数程序必须使用Fragments 必须实现的三个回调方法分别为:
onCreate
系统创建Fragments 时调用,可做执行初始化工作或者当程序被暂停或停止时用来恢复状态,跟Activity 中的onCreate相当。
onCreateView
用于首次绘制用户界面的回调方法,必须返回要创建的Fragments 视图UI。假如你不希望提供Fragments 用户界面则可以返回NULL。
onPause
当用户离开这个Fragments 的时候调用,这时你要提交任何应该持久的变化,因为用户可能不会回来。更多的事件可以参考上图的生命周期关系图。
Fragments 的类别
系统内置了三种Fragments ,这三种Fragments 分别有不同的应用场景分别为:
DialogFragment
对话框式的Fragments,可以将一个fragments 对话框并到activity 管理的fragments back stack 中,允许用户回到一个前曾摒弃fragments.
ListFragments
类似于ListActivity 的效果,并且还提供了ListActivity 类似的onListItemCLick和setListAdapter等功能。
PreferenceFragments
类似于PreferenceActivity .可以创建类似IPAD的设置界面。
Fragments 的详细使用
首先先来看一张DEMO 效果图:
使用:
1、创建两个fragment布局文件:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#00ff00"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="16dp"
android:text="切换" /> </RelativeLayout>
fragment2
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffff00"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变背景色"
android:textColor="#ff00ff"
/> </RelativeLayout>
main.xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<!-- 使用xml方式实现fragment -->
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragment.Fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragment2"
android:name="com.example.fragment.Fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>
2、创建fragment类,继承Fragment类:
package com.example.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button; public class Fragment1 extends Fragment { /**
* 当fragment被创建的时候调用的方法,返回当前fragment显示的内容
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//获取fragment1布局填充器
View view = inflater.inflate(R.layout.fragment1, null);
//获取fragment1上的button
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//通过获取activity的FragmentManager找到fragment2
Fragment2 fragment = (Fragment2) getActivity().getFragmentManager().findFragmentById(R.layout.fragment2);
fragment.setText("内容被改变了...");
System.out.println("Fragment1 button click .... "+fragment);
}
});
return view;
} }
package com.example.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class Fragment2 extends Fragment {
private TextView textView; /**
* 当fragment被创建的时候调用的方法,返回当前fragment显示的内容
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, null);
textView = (TextView) view.findViewById(R.id.textView1);
return view;
} public void setText(String text){
System.out.println("Fragment2 text respond .... ");
textView.setText(text);
}
}
package com.example.fragment; import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //dynamicFragment();
} /**
* 通过代码动态切换fragment
*/
private void dynamicFragment() {
//动态创建fragment
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight(); Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2(); FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction(); if(width>height){
//水平方向
ft.replace(android.R.id.content, fragment1);
}else {
ft.replace(android.R.id.content, fragment2);
}
ft.commit();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
上面的代码主要包含了fragment的动态创建(dynamicFragment方法)和两个fragment之间的通信。
Android学习笔记_46_fragment的简单应用的更多相关文章
- Android学习笔记:ListView简单应用--显示文字列表
在activity中的编写如下代码: final List<String> items = new ArrayList<String>(); //设置要显示的数据,这里因为是例 ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- Pro Android学习笔记 ActionBar(1):Home图标区
Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...
- 【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件
目录(?)[-] 在AIDL中定义服务接口 根据AIDL文件自动生成接口代码 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.n ...
- 【转】 Pro Android学习笔记(七七):服务(2):Local Service
目录(?)[-] Local service代码 调用Local ServiceLocal Service client代码 AndroidManifestxml定义Serviceacitivty的l ...
- 【转】 Pro Android学习笔记(七十):HTTP服务(4):SOAP/JSON/XML、异常
目录(?)[-] SOAP JSON和XMLPullParser Exception处理 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog. ...
- 【转】 Pro Android学习笔记(六九):HTTP服务(3):HTTP POST MultiPart
目录(?)[-] 建立测试环境 开发环境导入第三方JAR HTTP Post Multipart小例子 HTTP POST不仅可以通过键值对传递参数,还可以携带更为复杂的参数,例如文件.HTTP Po ...
随机推荐
- net.sf.json.JSONException: There is a cycle in the hierarchy! 转json死循环问题解决
解决上述问题遵照两个原则就可以: 1.页面不需要展示关联数据时 解决:将关联对象属性排除掉 2.页面需要展示关联数据时 解决:将关联对象改为立即加载,并且将关联对象中的属性排除
- Servlet3.0的文件上传功能
在Servlet3.0之前,文件上传需要借助于第三方插件,在Servlet3.0之后,Servlet本身开始支持文件上传功能. 获取上传的文件可以通过HTTPServletRequest的getPar ...
- 已知tomcat应用名称,如何查询该应用端口号
假设应用名称叫pear,并假设路径为:/datong/tomcat-pear/ 方法一,根据PID查找应用端口号,前提是应用已经运行 1,找到改应用的PID:ps -ef | grep pear | ...
- 为数据赋能:腾讯TDSQL分布式金融级数据库前沿技术
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 简介:李海翔,网名"那海蓝蓝",腾讯金融云数据库技术专家.中国人民大学信息学院工程硕士企业导师.著有<数据库事务处 ...
- Flask 框架理解(一)
Flask 框架理解(一) web 服务器 , web 框架 以及 WSGI 这里说的 web 服务器特指纯粹的 python HTTP 服务器(比如 Gunicorn,而不是 Apache,Ngin ...
- 配置phpstudy+phpstorm+xdebug环境
phpstorm版本: phpstudy版本: 具体的debug配置流程其实phpstorm里面已经明确的说过了,就是下图所示 所以 第一步: 首先要在phpstudy中开启xdebug扩展,所以要修 ...
- Django api
http://www.cnblogs.com/wulaoer/p/5276050.html
- django(6)model表语句操作、Form操作、序列化操作
1.model建表操作之创建索引.元数据 # 单表操作,创建表 class User(models.Model): name = models.CharField(max_length=32) ema ...
- SpringSecurity 3.2入门(3)单用户登录
1.增加web.xml文件配置如下 <!-- 获取Spring Security session的生命周期,这个监听器会在 session 创建和销毁的时候通知 Spring Security ...
- SOAP介绍,为学习WCF做准备
SOAP 1.什么是SOAP? 答:简单对象访问协议是交换数据的一种协议规范,是一种轻量的.简单的.基于XML的协议, 它被设计成在WEB上交换结构化的和固化的信息.WebService的三要素 SO ...