Fragment多用于平板中,Fragment当成Activity的一个界面的一个组成部分,Fragment有自己的生命周期,但是必须依托在Activity中。

参考链接

https://developer.android.com/guide/components/fragments.html?hl=zh-cn

Fragment 生命周期如下

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.fragmenttest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment 1" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment 2" /> <FrameLayout
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout> </LinearLayout> </RelativeLayout>

效果图如下:

创建Fragment1

Fragment1.java

package com.example.fragmenttest;

import android.app.Fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment1 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}

Fragment1布局

fragment_fragment1.xml

<FrameLayout 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"
tools:context="com.example.fragmenttest.Fragment1"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment 1" /> </FrameLayout>

创建Fragment

Fragment2.java

package com.example.fragmenttest;

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 {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment2, container, false);
}
}

fragment_fragment2.xml

<FrameLayout 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"
tools:context="com.example.fragmenttest.Fragment2"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment 2" /> </FrameLayout>

MainActivity.java

package com.example.fragmenttest;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity {
Button btn1, btn2; Fragment fr; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2); btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
} View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
fr = new Fragment1();
break;
case R.id.button2:
fr = new Fragment2();
break;
} FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// 将activity_main中的fragment转换成fragment1或者fragment2
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit(); }
}; }

管理fragment需要使用FragmentManager.使用getFragmentManager()获得。

操作fragemnt需要使用FragmentTransaction的api。使用beginTransaction()获得。

在用replace函数替换fragment.

应用到 Activity,您必须调用 commit()

运行情况,点击button1

点击button2

Activity通信

片段可以通过 getActivity() 访问 Activity 实例,并轻松地执行在 Activity 布局中查找视图等任务。

View listView = getActivity().findViewById(R.id.list);

Activity 也可以使用 findFragmentById() 或 findFragmentByTag(),通过从 FragmentManager 获取对 Fragment 的引用来调用片段中的方法。例如:

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

Tony Liu

2017-3-14, Shenzhen

android Fragment 笔记的更多相关文章

  1. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  2. 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版

    目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...

  3. Android编程权威指南笔记3:Android Fragment讲解与Android Studio中的依赖关系,如何添加依赖关系

    Android Fragment 当我在学习时,了解了Fragment词汇 Fragment是一种控制器对象,我就把所了解的简单说一下.activity可以派fragment完成一些任务,就是管理用户 ...

  4. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

  5. Fragment笔记整理

    前言 一直在用Fragment,但是没有系统的整理过,Google了一下相关文章,看到了几篇,将几篇还不错的文章重点整理了下,很多是直接Copy的,只为做个笔记,以后翻来看比较方便,建议大家看一下下面 ...

  6. 【转】Android开发笔记(序)写在前面的目录

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  7. [置顶] Android开发笔记(成长轨迹)

    分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...

  8. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  9. 【转】 Pro Android学习笔记(五一):ActionBar(4):标准和Tab模式

    之前,我们学习的Action Bar是标准模式,Tab模式的如下图所示. 对于Tab,我们在Android学习笔记(二二): 多页显示-Tag的使用中学习过,但Action Bar的tab更适合fra ...

随机推荐

  1. Linux进程学习 - 孤儿进程和守护进程

    孤儿进程和守护进程 通过前面的学习我们了解了如何通过fork()函数和vfork()函数来创建一个进程.现在 我们继续深入来学习两个特殊的进程:孤儿进程和守护进程 一.孤儿进程 1.什么是 孤儿进程如 ...

  2. 模拟HTTP请求获取返回值

    package org.jeecgframework.core.util; import java.io.BufferedReader; import java.io.IOException; imp ...

  3. mysqlbackup 重建带有gtid特性的slave

    一.mysqlbackup简介: mysqlbackup是mysql的一个企业级备份工具,优点就是牛逼,缺点就是这东西要钱买. 二.gtid 特性简介: gtid 的中文名叫全局事务ID,也就是说每一 ...

  4. php教程之Smarty模板用法实例

    分享下php之Smarty模板的使用方法. 剖析了smarty模板的使用方法,对于学习smarty的朋友有一定的参考价值. 详情如下: 一.模板中的注释每一个Smarty模板文件,都是通过Web前台语 ...

  5. SGMII 和 Serdes 的详细说明

    Serdes  : SERDES是英文SERializer(串行器)/DESerializer(解串器)的简称.它是一种时分多路复用(TDM).点对点的通信技术,即在发送端多路低速并行信号被转换成高速 ...

  6. 20.5 语音合成(百度2016年2月29日发布的tts引擎)

    分类:C#.Android.VS2015: 创建日期:2016-03-17 一.简介 编写手机App时,有时需要使用文字转语音(Text to Speech)的功能,比如开车时阅读收到的短信.导航语音 ...

  7. ViewPager滑动不畅及灵敏度的问题

    在项目中用到了Android的ViewPager组件,可是发如今滑动的时候不是特别流畅,有些小的滑动无法响应,于是考虑进行优化. 一開始考虑改动ViewPager中的一些參数.比方mTouchSlop ...

  8. 内网IP和外网IP

    1.什么是内网内网就是局域网,比如一个公司的局域网,局域网内每台计算机的IP地址是互异的,但不同局域网内的IP是可以相同的.2.什么是外网外网就是互联网,内网的所有计算机都是连接到一个外网IP,通过外 ...

  9. PairRDD中算子reduceByKey图解

    reduceByKey 函数原型: def reduceByKey(func: (V, V) => V): RDD[(K, V)] def reduceByKey(func: (V, V) =& ...

  10. Qt入门-layout布局

    开发一个图形界面应用程序,界面的布局影响到界面的美观.在设计一个界面之前,应该考虑到开发的界面可能给不用的用户使用,而用户的屏幕大小.纵横比例.分辨率可能不同,界面还可能是可缩放的,程序应该可以适应这 ...