zoukankan      html  css  js  c++  java
  • <Android基础> (四) Fragment Part 2

    4.3 Fragment的生命周期

    4.3.1 Fragment的状态和回调

    1.运行状态

    当一个Fragment是可见的,并且它关联的活动正处于运行状态是,该Fragment也处于运行状态

    2.暂停状态

    当一个活动进入了暂停状态时(由于另一个未占满屏幕的活动被添加到了栈顶),与它相关的可见Fragment就会进入暂停状态

    3.停止状态

    当一个活动进入了停止状态,与它相关联的Fragment就会进入到停止状态,或者通过调用FragmentTransaction的remove()、replace()方法将Fragment从活动中移除。

    总的来说,进入停止状态的Fragment对用户来说时完全不可见的。

    4.销毁状态

    当活动被销毁时,与它相关联的Fragment就会进入到销毁状态。或者调用FragmentTransaction的remove()、replace()方法将Fragmet从活动中移除。

    在事务提交之前没有调用addToBackStack()方法,也会使Fragment进入到销毁状态。

    Fragment的几个回调方法

    onAttach() 当Fragment和活动建立关联的时候调用。

    onCreateView() 当Fragment创建视图(加载布局)时调用。

    onActivityCreated() 确保与Fragment相关联的活动一定已经创建完毕的时候调用。

    onDestroyView() 当与Fragment相关联的视图被移除的时候调用。

    onDetach() 当Fragment与活动解除关联的时候调用。

    Fragment完整的生命周期示意图:

    4.4 动态加载布局的技巧

    4.4.1 使用限定符

    运行程序判断应该使用单页模式还是双页模式,需要借助限定符(Qualifiers)来实现

    Eg:

    1.修改FragmentTest中的activity_main.xml文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/left_fragment"
            android:name="com.example.song.fragmenttest.LeftFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
        
    </LinearLayout>

    只留下一个左侧Fragment并让它充满父布局。

    2.在res目录下新建layout-large文件夹,并新建一个布局也为activity-main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/left_fragment"
            android:name="com.example.song.fragmenttest.LeftFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    
        <fragment
            android:id="@+id/right_fragment"
            android:name="com.example.song.fragmenttest.RightFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"/>
    
    </LinearLayout>

    3.layout/activity_main布局只包含一个Fragment,即单页模式。layout-large/activity_main布局包含了两个Fragment,即双页模式。

    large即为一个限定符,屏幕被认为是large的设备就会自动加载layout-large文件夹下的布局

    小屏幕设备则还是会加载layout文件夹下的布局

    注释掉MainActivity中replaceFragment()方法里的代码

    运行程序:

    平板和手机显示的分别为双页模式和单页模式

    4.4.2 使用最小宽度限定符

    最小宽度限定符(Smallest-width-Qualifier)允许对屏幕的宽度指定一个最小值(以dp为单位),然后以这个最小值为临界点,屏幕宽度大于这个值就加载一个布局,小于这个值的设备就加载另一个布局。

    res目录下新建layout-sw600dp文件夹,然后新建activity_layout.xml布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/left_fragment"
            android:name="com.example.song.fragmenttest.LeftFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    
        <fragment
            android:id="@+id/right_fragment"
            android:name="com.example.song.fragmenttest.RightFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3" />
    </LinearLayout>

    当程序运行的屏幕大于等于600dp时,会加载layout-600dp/activity-main.xml

    当程序运行的屏幕小于600dp时,会加载默认的layout/activity-main.xml

  • 相关阅读:
    linux 常用命令(个人记录)
    jmeter 5.0版本更新说明(个人做个记录)
    Netdata---Linux系统性能实时监控平台部署记录
    MySQL Yum存储库 安装、升级、集群
    linux 各项配置汇总
    构建Maven项目自动下载jar包
    计算服务器的pv量算法
    性能计算公式
    jstack(查看线程)、jmap(查看内存)和jstat(性能分析)命令
    结构模式
  • 原文地址:https://www.cnblogs.com/HarSong13/p/10674990.html
Copyright © 2011-2022 走看看