一、使用Fragment

1.AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.learningfragment"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

2.布局文件

(1)activity_main.xml

其中放置的是一个帧布局的布局容器,其id为container

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/container">
</FrameLayout>

(2)fragment_main.xml

主fargment布局文件,里面放置的按钮用于弹出另外一个fragment

<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"
android:orientation="vertical"
tools:context="com.example.shiyanshi.learningfragment.MainActivityFragment"
tools:showIn="@layout/activity_main"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京欢迎您!" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnShowFragmentAnother"
android:text="显现另外一个Fragment"/> </LinearLayout>

(3)fragment_another.xml

从fragment布局文件,里面的按钮用于支持返回到上一个fragment界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentAnother"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是另外一个Fragment"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnBackOperation"
android:text="后退操作"/> </LinearLayout>

3.Java源文件

(1)MainActivity.java

package com.example.shiyanshi.learningfragment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if(savedInstanceState==null){
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container,new MainActivityFragment()) //add的第一个参数是布局容器,第二个参数是主Fragment的类
.commit();
}

    }

}

(2)MainActivityFragment.java

package com.example.shiyanshi.learningfragment;

import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends

Fragment

 {      //注意其继承的是

import android.support.v4.app.Fragment

中的Fragment

    public MainActivityFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView=inflater.inflate(R.layout.fragment_main, container, false); //第一个参数:主fragment的布局文件,第二个:布局容器,第三个为布尔型attachToRoot

rootView.findViewById(R.id.btnShowFragmentAnother).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.addToBackStack(null) //加入后退栈,使其支持后退的功能
.replace(R.id.container,new AnotherFragment())
.commit();
}
});

        return rootView;
}
}

(3)AnotherFragment.java

package com.example.shiyanshi.learningfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by shiyanshi on 2016/1/25.
*/
public class AnotherFragment extends

Fragment

 {
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_another,container,false);

view.findViewById(R.id.btnBackOperation).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().popBackStack();
}
});

        return view;
}
}

4.界面显示效果


二、Fragment的生命周期



												

02Android用户界面优化之(一)Android Fragment的更多相关文章

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

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

  2. Android: Fragment编程指南

    本文来自于www.lanttor.org Fragment代表了Activity里的一个行为,或者Activity UI的一部分.你可以在一个activity里构造多个Fragment,也可以在多个a ...

  3. 【Android自学日记】【转】Android Fragment 真正的完全解析(下)

    上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API,如果你还不了解,请看:Android Fragment 真正的完全解析(上). 本篇将介绍上篇博客提到的:如何管理Frag ...

  4. Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理

    Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...

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

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

  6. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

  7. Android Fragment使用(一) 基础篇 温故知新

    Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...

  8. Android Fragment应用实战

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

  9. Android Fragment 真正的完全解析(下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...

随机推荐

  1. Android 四大组件之 Activity

    1 简介 Activity (活动) 即应用程序 显示的 界面.可以通过两种方式 设置显示的内容 1:纯代码方式 2:xml 布局方式 无论哪一种方式,都是通过 setContentView 来设置显 ...

  2. CentOS 5.7 中文乱码问题解决方案

    一.安装中文支持: # yum install "@Chinese Support" 二.用 yum 安装中文字体 #yum install fonts-chinese.noarc ...

  3. Android服务端本地窗口FramebufferNativeWindow

    Android窗口系统 我们知道Android系统采用OpenGL来绘制3D图形,OpenGL ES提供了本地窗口(NativeWindow)的概念,无论是在Android平台中还是其他平台中,只要实 ...

  4. 随学随记之java的数据类型

    Java中的变量只有两种数据类型:基本数据类型(8种).引用数据类型 定义变量时内存中的状态变化:定义数据类型,开辟空间,存放数据. 8种基本数据类型的变量各占多大的内存空间,变量的取值范围 byte ...

  5. C++程序设计实践指导1.9统计与替换字符串中的关键字改写要求实现

    改写要求1:将字符数组str改为字符指针p,动态开辟存储空间 改写要求2:增加统计关键字个数的函数void CountKeyWords() 改写要求3: 增加替换函数void FindKeyWords ...

  6. hdu 4612 Warm up(无向图Tarjan+树的直径)

    题意:有N个点,M条边(有重边)的无向图,这样图中会可能有桥,问加一条边后,使桥最少,求该桥树. 思路:这个标准想法很好想到,缩点后,求出图中的桥的个数,然后重建图必为树,求出树的最长直径,在该直径的 ...

  7. Oracle表分区[转]

    废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: create table gra ...

  8. Canvas基础学习(一)——实现简单时钟显示

    HTML5最受欢迎的功能就是<canvas>元素.这个元素负责在页面中设定一个区域,然后就可以通过JavaScript动态地在这个区域中绘制图形.关于<canvas>元素的一些 ...

  9. API例子:用Python驱动Firefox采集网页数据

    1,引言 本文讲解怎样用Python驱动Firefox浏览器写一个简易的网页数据采集器.开源Python即时网络爬虫项目将与Scrapy(基于twisted的异步网络框架)集成,所以本例将使用Scra ...

  10. C语言基础09

    指向结构体变量的指针叫做结构体指针: typedef struct { int num; char name[30];    // char *name; 程序会崩溃,*name本身是指针,没有什么空 ...