Android Fragment学习笔记

目录
1.Fragment 诞生初衷

    自从Android 3.0中引入fragment的概念,根据词海的翻译可以译为:碎片、片段。其上的是为了解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件,而多出来的空间存放UI使其会产生更多的交互,从而诞生了fragment。fragments 的设计不需要你来亲自管理view hierarchy 的复杂变化,通过将Activity 的布局分散到frament 中,可以在运行时修改activity 的外观,并且由activity 管理的back stack 中保存些变化。

2.Fragment 设计理念

  在设计应用时特别是Android 应用 ,有众多的分辨率要去适应,而fragment可以让你在屏幕不同的屏幕上动态管理UI。例如:通讯应用程序(QQ),用户列表可以在左边,消息窗口在右边的设计。而在手机屏幕用户列表填充屏幕当点击某一用户时,则弹出对话窗口的设计,如下图:

3.Fragment 生命周期

每一个fragment 都有自己的一套生命周期回调方法和处理自己的用户输入事件。 对应生命周期可参考下图:

1.onAttach() 当Fragment附加到Activity上调用。

2.onCreate() Fragment初始化操作

3.onCreateView() 用于首次绘制用户界面的回调方法,必须返回要创建的Fragments 视图UI。假如你不希望提供Fragments 用户界面则可以返回NULL。

4.onActivityCreated() 父Activity和Fragment的UI都被完全创建完后调用。

5.onDestoryView() Fragment的UI被分离时调用。

6.onDatch() Fragment从父Activity身上分离的时候调用。

7.onPause() 当用户离开这个Fragments 的时候调用,这时你要提交任何应该持久的变化,因为用户可能不会回来。

4.Fragment 示例

4.1 创建Fragment 继承Fragment类

package com.malinkang.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FirstFragment extends Fragment {

    @Override
    /**
     * inflater用于向fragment中添加view
     * container用来存放fragment其layout的ViewGroup对象
     * savedInstanceState类似于onCreate()中相同的Bundle参数,用于恢复状态
     */
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /**
         * inflater的inflate方法的三个参数分别是:
         * 
         * int resource:fragment的layout资源ID。
         * 
         * ViewGroup root:存放fragment的layout的ViewGroup
         * 
         * boolean attachToRoot:是否在创建fragment的layout时,把layout添加到container上面去
         */
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

}

fragment_first.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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一个Fragment" />

</LinearLayout>

4.2 添加Fragment到UI界面中
添加到UI界面有两种方式:1.在xml文件中使用<fragment>标签声明 2.在Java代码中将fragment添加到一个ViewGroup

本例采用第一种方式

<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:orientation="vertical"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/fragment_1"
        android:name="com.malinkang.fragment.FirstFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/fragment_2"
        android:name="com.malinkang.fragment.FirstFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    }

}

 参看:http://developer.android.com/intl/zh-CN/training/basics/fragments/creating.html

5.管理Fragment

FragmentManager:提供方法去获取使用Fragment和执行Fragment事务。

FragmentTransaction:事务可以用来增加,移除,替换Fragment。

 http://www.vogella.com/articles/AndroidFragments/article.html

原文地址:https://www.cnblogs.com/malinkang/p/3044396.html