Fragment初识

简介: Fragment初识

概述

官方API


Fragment是什么


Android 在 Android 3.0(API 11 级)中引入了Fragment,主要是为了给大屏幕(如平板电脑)上更加动态和灵活的 UI 设计提供支持。由于平板电脑的屏幕比手机屏幕大得多,因此可用于组合和交换 UI 组件的空间更大。利用片段实现此类设计时,您无需管理对视图层次结构的复杂更改。 通过将 Activity 布局分成片段,您可以在运行时修改 Activity 的外观,并在由 Activity 管理的返回栈中保留这些更改。


当然了我们普通手机开发也会加入这个Fragment, 我们可以把它看成一个小型的Activity,又称Activity片段!


例如:新闻应用可以使用一个片段在左侧显示文章列表,使用另一个片段在右侧显示文章—两个片段并排显示在一个 Activity 中,每个片段都具有自己的一套生命周期回调方法,并各自处理自己的用户输入事件。 因此,用户不需要使用一个 Activity 来选择文章,然后使用另一个 Activity 来阅读文章,而是可以在同一个 Activity 内选择文章并进行阅读,如下图中的左侧平板电脑布局所示。


我们应该将每个片段都设计为可重复使用的模块化 Activity 组件。也就是说,由于每个片段都会通过各自的生命周期回调来定义其自己的布局和行为,您可以将一个片段加入多个 Activity,因此,您应该采用可复用式设计,避免直接从某个片段直接操纵另一个片段。 这特别重要,因为模块化片段让您可以通过更改片段的组合方式来适应不同的屏幕尺寸。 在设计可同时支持平板电脑和手机的应用时,您可以在不同的布局配置中重复使用您的片段,以根据可用的屏幕空间优化用户体验。 例如,在手机上,如果不能在同一 Activity 内储存多个片段,可能必须利用单独片段来实现单窗格 UI。


下图是文档中给出的一个Fragment分别对应手机与平板间不同情况的处理图:


20160314233909600.png


例如:仍然以新闻应用为例—在平板电脑尺寸的设备上运行时,该应用可以在Activity A 中嵌入两个片段。不过,在手机尺寸的屏幕上,没有足以储存两个片段的空间,因此Activity A 只包括用于显示文章列表的片段,当用户选择文章时,它会启动Activity B,其中包括用于阅读文章的第二个片段。因此,应用可通过重复使用不同组合的片段来同时支持平板电脑和手机,如上图右侧。


如需了解有关通过利用不同片段组合来适应不同屏幕配置这种方法设计应用的详细信息,请参阅支持平板电脑和手机指南。


Fragment的生命周期图

20160314234014796.jpg


20160314234059859.png



20160315224753614.png

①Activity加载Fragment的时候,依次调用下面的方法: onAttach -> onCreate -> onCreateView -> onActivityCreated -> onStart ->onResume

②当我们弄出一个悬浮的对话框风格的Activity,或者其他,就是让Fragment所在的Activity可见,但不获得焦点 onPause

③当对话框关闭,Activity又获得了焦点: onResume

④当我们替换Fragment,并调用addToBackStack()将他添加到Back栈中 onPause -> onStop -> onDestoryView !!注意,此时的Fragment还没有被销毁哦!!!

⑤当我们按下键盘的回退键,Fragment会再次显示出来: onCreateView -> onActivityCreated -> onStart -> onResume

⑥如果我们替换后,在事务commit之前没有调用addToBackStack()方法将 Fragment添加到back栈中的话;又或者退出了Activity的话,那么Fragment将会被完全结束, Fragment会进入销毁状态 onPause -> onStop -> onDestoryView -> onDestory -> onDetach


核心要点


3.0版本后引入,即minSdk要大于11,使用兼容包v4,可以向下兼容

Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套 的Fragment也是需要嵌套在Activity中的,间接地说,Fragment还是需要嵌套在Activity中!! 受寄主Activity的生命周期影响,当然他也有自己的生命周期!另外不建议在Fragment里面 嵌套Fragment因为嵌套在里面的Fragment生命周期不可控!!!

官方文档说创建Fragment时至少需要实现三个方法:onCreate( ),onCreateView( ),OnPause( ); 不过貌似只写一个onCreateView也是可以的…

Fragment的生命周期和Activity有点类似:

三种状态:

Resumed:在允许中的Fragment可见

Paused:所在Activity可见,但是得不到焦点

Stoped: ①调用addToBackStack(),Fragment被添加到Bcak栈 ②该Activity转向后台,或者该Fragment被替换/删除

ps:停止状态的fragment仍然活着(所有状态和成员信息被系统保持着),然而,它对用户 不再可见,并且如果activity被干掉,他也会被干掉.


Fragment的几个子类


很多时候我们都是直接重写Fragment,inflate加载布局完成相应业务了,子类用的不多,等需要的 时候在深入研究!


对话框:DialogFragment

列表:ListFragment

选项设置:PreferenceFragment

WebView界面:WebViewFragment


是用App包下的Fragment还是v4包下的


问题概述:

再引入Fragment声明时,


20160314234600830.png


我们到底是使用android.app下的Fragment还是用的android.support.v4.app包下 的Fragment呢?


其实都可以,前面说过Fragment是Android 3.0(API 11)后引入的,那么如果开发的app需要 在3.0以下的版本运行呢?比如还有一点点市场份额的2.3!于是乎,v4包就这样应运而生了, 而最低可以兼容到1.6版本!至于使用哪个包看你的需求了,现在3.0下手机市场份额其实已经不多了,随街都是4.0以上的,7.0都出了,你说呢…所以这个时候,你可以直接使用app包下的Fragment 然后调用相关的方法,通常都是不会有什么问题的;如果你Fragment用了app包的, FragmentManager和FragmentTransaction都需要是app包的!要么用全部用app,要么全部用v4, 不然可是会报错的哦!当然如果你要自己的app对于低版本的手机也兼容的话,那么就可以选择用v4包!


使用v4包下Fragment要注意的地方:

①如果你使用了v4包下的Fragment,那么所在的那个Activity就要继承FragmentActivity或者其子类如AppCompatActivity

案例:今天在xml文件中静态地载入fragment,然后重写了Fragment,但是在加载Activity的时候就报错了, 大概的提示就是Fragment错误还是找不到什么的,name属性改了几次还是错!最后才发现是用了 v4的包的缘故,只需让自己的Activity改成FragmentActivity即可!

如果引用的是V4包中的类,getFragmentManager( )不能使用,需要改成getSupportFragmentManager( )


创建一个Fragment


静态加载Fragment


操作步骤

20160315002854947.png

Step 1:定义Fragment的布局

Step 2:自定义一个Fragment类,需要继承Fragment或者他的子类,重写onCreateView()方法 在该方法中调用:inflater.inflate()方法加载Fragment的布局文件,接着返回加载的view对象

Step 3:在需要加载Fragment的Activity对应的布局文件中添加fragment的标签, 记住,name属性是全限定类名,就是要包含Fragment的包名,另外 fragment必须用id或tag作为唯一标识

Step 4: Activity在onCreate( )方法中调用setContentView()加载布局文件即可!


Code

Fragment的UI布局fragment_static_load.xml

<?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">
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是静态加载的Fragment"/>
</LinearLayout>


创建类FragmentOne.java,继承Fragment或者其之类

package com.turing.base.activity.fragment;
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;
import com.turing.base.R;
public class FragmentOne extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_static_load,container,false);
        return view;
    }
}


在Activity的布局文件activity_fragment_static_load.xml中声明fragment标签

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/fragmentStaticLoad"
        android:name="com.turing.base.activity.fragment.FragmentOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>


加载布局文件,操作UI

package com.turing.base.activity.fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.turing.base.R;
/**
 * 静态加载Fragment的Activity
 */
public class FragmentStaticLoadAct extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_static_load);
        //静态加载时可以直接获取到 Fragment中的UI控件
        TextView tv = (TextView) findViewById(R.id.textview);
        tv.setText("我在Act中获取到了Fragment中的UI控件");
    }
}


效果图

操作步骤


20160315105702769.gif

动态加载Fragment


实现动态加载,我们需要先了解Fragment事务。


Fragment事务:对Fragment进行添加、移除、替换或执行其它动作,提交给Activity的每一个变化。


Fragment是UI模块,自然在一个Activity中可以不只有一个模块,所以Android提供了FragmentManage类来管理Fragment,FragmentTransaction类来管理事务。


我们对Fragment的动态加载就是先将添加、移除等操作提交到事务,然后通过FragmentManage完成的。


通过FragmentManager.beginTransaction()我们可以开始一个事务。在事务中,我们可以对Fragment进行的操作以及对应的方法如下:


添加:add()

移除:remove()

替换:replace()

提交事务:commit()

上面几个是比较常用的,还有attach()、detach()、hide()、addToBackStack()等方法。

如果允许用户通过back键退回到前一个Fragment状态,调用commit()之前可以加入addToBackStack()方法


我们需要注意的是,Fragment以ID或Tag作为唯一标识,所以remove和replace的参数是Fragment,这个Fragment目标Fragment一致


注意:Activity动态的添加Fragment必需有一个容器View来容纳Fragment的layout布局


操作步骤

20160315110455490.png

Code

activity_fragment_dynamic_load

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<!--Activity动态的添加Fragment必需有一个容器View来容纳Fragment的layout布局-->
    <LinearLayout
        android:id="@+id/fragmentDynamicLoad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"/>
</RelativeLayout>


FragmentDynamicLoadAct.java

package com.turing.base.activity.fragment.dynamicload;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.TextView;
import com.turing.base.R;
public class FragmentDynamicLoadAct extends AppCompatActivity {
    // 屏幕宽高
    int width, height;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_dynamic_load);
        // 获取屏幕的宽高
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        width = displayMetrics.widthPixels;
        height = displayMetrics.heightPixels;
        // 根据宽高,加载不同的Fragment
        if (width < height) {
            FragementFirst fragementFirst = new FragementFirst();
            // 使用V4的包就使用getSupportFragmentManager ,使用app下的,就使用getFragmentManager
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragmentDynamicLoad, fragementFirst)
                    .commit();
        } else {
            FragmentSecond fragmentSecond = new FragmentSecond();
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragmentDynamicLoad, fragmentSecond)
                    .commit();
        }
        //当fragment被提交之后,【fragmentTransaction.commit()提交fragment是异步处理的,所以获取fragment时要注意】
        // 可通过以下两种方法获取fragment:findFragmentByTag()、findFragmentById()
    }
    /**
     * 重写onStart()方法,
     * 因为从fragment的生命周期可以知道当Activity的onCreate(Bundle savedInstanceState)中
     * 还无法获取fragment的布局的组件
     */
    @Override
    protected void onStart() {
        super.onStart();
        /**
         * 可以直接通过findViewById()获取fragment的组件,
         * 因为fragment本身就是Activity的一部分(“碎片”/“片段”);
         * 因为Activity和fragment要从fragment的onActivityCreate()生命周期方法之后
         * 才能相互获取对方布局中的组件,
         * 所以在fragment中获取Activity的组件最早只能在onActivityCreate()中获取,
         * 而Activity最早只能在onStart()中获取;
         */
        // 获取Fragment中的UI组件
        if (width < height) {
            TextView textView = (TextView) findViewById(R.id.fragmentFirst);
            textView.setText("~~~~~First");
        } else {
            TextView textView = (TextView) findViewById(R.id.fragmentSecond);
            textView.setText("~~~~~Second");
        }
    }
}


FragementFirst.java

package com.turing.base.activity.fragment.dynamicload;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.turing.base.R;
/**
 * A simple {@link Fragment} subclass.
 */
public class FragementFirst extends Fragment {
    public FragementFirst() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragement_first, container, false);
    }
}


fragment_fragement_first.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.turing.base.activity.fragment.dynamicload.FragementFirst">
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fragmentFirst"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment first" />
</FrameLayout>

FragmentSecond.java

package com.turing.base.activity.fragment.dynamicload;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.turing.base.R;
/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentSecond extends Fragment {
    public FragmentSecond() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_second, container, false);
    }
}

fragment_fragment_second.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.turing.base.activity.fragment.dynamicload.FragmentSecond">
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fragmentSecond"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment second" />
</FrameLayout>


效果图

20160315172120280.gif


Fragment管理与Fragment事务


20160315161726150.png


Fragment与Activity的交互


20160315161656306.png


组件获取

Activity中获取Fragment,以及Fragment中的组件

获取Fragment

当fragment被提交之后,【fragmentTransaction.commit()提交fragment是异步处理的,所以获取fragment时要注意】

可通过以下两种方法获取fragment:findFragmentByTag()、findFragmentById()


Fragment中的组件

 /**
     * 重写onStart()方法,
     * 因为从fragment的生命周期可以知道当Activity的onCreate(Bundle savedInstanceState)中
     * 还无法获取fragment的布局的组件
     */
    @Override
    protected void onStart() {
        super.onStart();
        /**
         * 可以直接通过findViewById()获取fragment的组件,
         * 因为fragment本身就是Activity的一部分(“碎片”/“片段”);
         * 因为Activity和fragment要从fragment的onActivityCreate()生命周期方法之后
         * 才能相互获取对方布局中的组件,
         * 所以在fragment中获取Activity的组件最早只能在onActivityCreate()中获取,
         * 而Activity最早只能在onStart()中获取;
         */
        // 获取Fragment中的UI组件
        if (width < height) {
            TextView textView = (TextView) findViewById(R.id.fragmentFirst);
            textView.setText("~~~~~First");
        } else {
            TextView textView = (TextView) findViewById(R.id.fragmentSecond);
            textView.setText("~~~~~Second");
        }
    }


Fragment中获取Activity中的组件

public class FragementFirst extends Fragment {
    public FragementFirst() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragement_first, container, false);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 在Fragment中获取Activity的组件
        TextView textView = (TextView) getActivity().findViewById(R.id.id_tv_actUI);
        textView.setText("FFFF");
    }
}



数据传递

①Activit传递数据给Fragment:


在Activity中创建Bundle数据包,调用Fragment实例的setArguments(bundle) 从而将Bundle数据包传给Fragment,然后Fragment中调用getArguments获得 Bundle对象,然后进行解析就可以了

            fragementFirst = new FragementFirst();
            // 使用V4的包就使用getSupportFragmentManager ,如果使用app下的Fragment,就使用getFragmentManager
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragmentDynamicLoad, fragementFirst)
                    .commit();
            // Activity传递数据给Fragment
            Bundle bundle = new Bundle();
            bundle.putString("key", "这是Activity传递给Fragment的数据");
            // setArguments
            fragementFirst.setArguments(bundle);

在Fragment中接收解析数据

  // 接收Activity传递过来的数据
        Bundle bundle = getArguments();
        Toast.makeText(getActivity(), bundle.getString("key"), Toast.LENGTH_SHORT).show();

20160315210035405.gif

②Fragment传递数据给Activity:

在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口, Fragment就可以通过回调接口传数据了。


Step 1:定义一个回调接口:(Fragment中)

FragementFirst.java

/**
     * 定义一个回调接口:(Fragment中)
     */
    public interface FragmentCallBack {
        //定义一个接口方法
        void getResult(String result);
    }


Step 2:接口回调(Fragment中)

FragementFirst.java

getData改方法持有接口对象


  /**
     * 接口回调(Fragment中)
     */
    public void getData(FragmentCallBack callBack){
        // 模拟获取的数据
        String msg = "小工匠";
        // 接口回调
        callBack.getResult(msg);
    }


Step 3:使用接口回调方法读数据(Activity中)

 //使用接口回调方法读数据(Activity中)
            fragementFirst.getData(new FragementFirst.FragmentCallBack() {
                @Override
                public void getResult(String result) {
                    Toast.makeText(FragmentDynamicLoadAct.this, result, Toast.LENGTH_SHORT).show();
                }
            });


总结

->在Fragment定义一个接口,接口中定义抽象方法,你要传什么类型的数据参数就设置为什么类型;

->接着还有写一个调用接口中的抽象方法,把要传递的数据传过去

->再接着就是Activity了,调用Fragment提供的那个方法,然后重写抽象方法的时候进行数据 的读取就可以了~


运行图

20160316095424642.gif


③Fragment与Fragment之间的数据互传


找到要接受数据的fragment对象,直接调用setArguments传数据进去就可以了

通常的话是replace时,即fragment跳转的时候传数据的,那么只需要在初始化要跳转的Fragment

后调用他的setArguments方法传入数据即可!

如果是两个Fragment需要即时传数据,而非跳转的话,就需要先在Activity获得f1传过来的数据,

再传到f2了,就是以Activity为媒介~

FragmentManager fManager = getSupportFragmentManager( );
FragmentTransaction fTransaction = fManager.beginTransaction();
Fragmentthree t1 = new Fragmentthree();
Fragmenttwo t2 = new Fragmenttwo();
Bundle bundle = new Bundle();
bundle.putString("key",id);
t2.setArguments(bundle); 
fTransaction.add(R.id.fragmentRoot, t2, "~~~");  
fTransaction.addToBackStack(t1);  
fTransaction.commit(); 

还有一片简书上的文章 可以学习下~

相关文章
|
29天前
|
Android开发
RecyclerView的简单使用在activity和fragment中
RecyclerView的简单使用在activity和fragment中
21 0
|
9月前
|
XML 缓存 Android开发
QMUI实战(二)—Activity 和 Fragment,我们该选择谁?
在一开始,官方只提供了 Activity 来作为 UI 界面的载体,因此我们也别无选择,只能用它。而在 Android 3.0 后,Fragment 也面世了,它一开始是用于适配平板的,以邮件列表与详情的适配为例,手机端够小,因此开始展示列表,点击进入详情,而平板够大,则可以列表显示在左侧,详情显示在右侧,点击列表只是切换详情。对于这种适配场景,列表页和详情页必须在同一个 Activity 里了,而这便是我所知道的 Fragment 诞生的场景了。
116 0
|
XML ARouter API
Fragivity:像使用Activity一样使用Fragment
近年来,SPA,即单Activity架构逐渐开始受到欢迎,随之而生了很多优秀的三方库,大部分是基于Fragment作为实现方案,Fragivity 使用 Fragment + Navigatiion 打造最好用的 SPA 框架
417 0
|
Java
为Fragment也写一个ViewInject
title: 为Fragment也写一个ViewInject date: 2018-10-28 11:29:51 tags: fragment ViewInject 1.简介 鸿洋博客:https://blog.csdn.net/lmj623565791/article/details/39269193 前段时间根据鸿洋的博客,写了一个ViewInject,本以为可以和findViewById说再见了,但是今天在fragment中使用ViewInject,却报了空指针的错误,原来是控件没有实例化就调用了。
1123 0
|
Android开发 开发者 容器
Fragment详解
前言 谢谢你那么的、安静的捧场。 Fragment的概述及其设计初衷 Fragment代表了Activity的子模块,因此可以把Fragment理解成Activity片段。
1129 0
|
XML Android开发 数据格式
|
XML API Android开发
怎么知道Fragment属于哪个Activity呢?
如果程序是一条线运行的,Fragment 中 getActivity() 是获取的上一个打开或者执行的Activity中的值。   多个Activity也是如此,就看顺序是怎么执行的,getActivity获得的是当前Activity的上下文,也就是上一个Activity的值。
1125 0