一、使用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. Linux以及Android开发中的小技巧和长繁命令记录收集

    不断更新收集中.... 201407161654 ssh以nx_guest的身份登录到172.24.221.137,然后在172.24.221.137与172.24.61.252的8080port建立 ...

  2. PCL库配置出现的问题(WIN10+VS2013)

    边看电影边配终于配好了,中间出现了一些问题,在网上很难搜到,可能每个人都碰到的不同.摸索了一会终于都解决了,记录在这里,免得又碰到. PCL是什么东西就不在此介绍了. 主要是参考这篇博客做得,不过我后 ...

  3. C#自定义控件在添加引用后不显示在工具箱的解决方法

    先说一些背景: 在开发C#项目时,发现很多控件存在复用的情况,控件的属性都是要设置成一样的,我就想,能不能设置一个类来存放这个控件,这样我每次用的时候直接加一些特殊的操作就可以了,不需要再次设置控件属 ...

  4. 004 range的用法

  5. javascript 高级程序设计学习笔记(面向对象的程序设计) 2

    在调用构造函数时会为实例添加一个指向最初原型的指针,我们可以随时为原型添加属性和方法,并且能在实例中体现出来,但如果是重新了原型对象,那就会切断构造函数与最初原型的联系. function Dog ( ...

  6. 天坑 之 java web servlet+jsp项目 配置后 404 (MyEclipse转eclipse)

    最近搞一个自己的博客系统玩,用了servlet+jsp,结果发现了两个大问题: 1.无法 Export 出 WAR文件: 2.生成WAR,放置到TOMCAT的 webapps目录后,http://lo ...

  7. 日志管理-NLog日志框架简写用法

    本文转载:http://www.blogjava.net/qiyadeng/archive/2013/02/27/395799.html 在.net中也有非常多的日志工具,今天介绍下NLog.NLog ...

  8. hibernate联合主键注解配置

    在网上看到好多方法,结果拿来用还是出现了一些问题.现在整理一下 1.主键类 import javax.persistence.Column; public class UserRoleUionPK i ...

  9. js监控视频播放的事件并打印log

    html代码: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" ...

  10. Shader的使用

    一.LinearGradient 步骤:①.创建LinearGradient  步骤   ②.将其加入到Paint 步骤一: 构造LinearGradient的参数 public LinearGrad ...