zoukankan      html  css  js  c++  java
  • fragment和fragmentactivity解析

    一、为什么要使用Fragment 

    1、当我们须要动态的多界面切换的时候,就须要将UI元素和Activity融合成一个模块。在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个界面动态改变。在4.0或以上系统中就能够使用新的特性来方便的达到这个效果--Fragment类。Fragment类似一个嵌套Activity,能够定义自己的layout和自己的生命周期。

    2、 多个Fragment能够放在一个Activity中(所以上面讲到类似一个嵌套Activity)。而这个类能够对这些Fragment进行配置以适应不同的屏幕尺寸(比方平板和手机)。

    二、使用Fragment 

    1、Fragment 是 activity 的界面中的一部分或一种行为。

    能够把多个 Fragment 组合到一个 activity 中来创建一 个多面界面而且能够在多个 activity 中重用一个 Fragment。

    能够把 Fragment 觉得模块化的一段 activity,它具 有自己的生命周期,接收它自己的事件,并能够在 activity 执行时被加入或删除。

    2、Fragment 不能独立存在,它必须嵌入到 activity 中,并且 Fragment 的生命周期直接受所在的 activity 的影 响。

    3、当向 activity 中加入一个 Fragment 时,它须置于 ViewGroup 控件中,而且需定义 Fragment 自己的界面。

    可 以在 layoutxml 文件里声明 Fragment,元素为:<fragment>;也能够在代码中创建 Fragment,然后把它加入到 ViewGroup 控件中。

    然而,Fragment 不一定非要放在 activity 的界面中,它能够隐藏在后台为 actvitiy 工作。

    三、 生命周期

    通常, 应当至少实现例如以下的生命周期方法:

    onCreate()
    当创建fragment时, 系统调用该方法. 

    在实现代码中,应当初始化想要在fragment中保持的必要组件, 当fragment被暂停或者停止后能够恢复.

    onCreateView()
    fragment第一次绘制它的用户界面的时候, 系统会调用此方法. 为了绘制fragment的UI,此方法必须返回一个View, 这个view是你的fragment布局的根view. 假设fragment不提供UI, 能够返回null.

    onPause()

    用户将要离开fragment时,系统调用这种方法作为第一个指示(然而它不总是意味着fragment将被销毁.) 在当前用户会话结束之前,通常应当在这里提交不论什么应该持久化的变化(由于用户有可能不会返回).

    大多数程序应最少对 fragment 实现这三个方法。

    当然还有其他几个回调方法可应该按情况实现之。


    下图为 fragment 的生命周期(它所在的 activity 处于执行状态)。


    四、怎样使用Fragment 

    1、加入一个用户界面   
             fragment通经常使用来作为一个activity的用户界面的一部分,并将它的layout提供给activity.为了给一个fragment提供一 个layout,你必须实现 onCreateView()回调方法, 当到了fragment绘制它自己的layout的时候,Android系统调用它.你的此方法的实现代码必须返回一个你的fragment的 layout的根view. 

           从onCreateView()返回的View, 也能够从一个layout的xml资源文件里读取并生成. 为了帮助你这么做, onCreateView() 提供了一个LayoutInflater 对象.

          举个样例, 这里有一个Fragment的子类, 从文件 frament_main.xml 载入了一个layout:

    	@Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    			Bundle savedInstanceState) {
    		View view = inflater.inflate(R.layout.frament_main, container, false);
    		return view;
    	}
    PS
    传入onCreateView()的container參数是你的fragmentlayout将被插入的父ViewGroup(来自activity的layout)  savedInstanceState 參数是一个Bundle, 假设fragment是被恢复的,它提供关于fragment的之前的实例的数据,

    inflate() 方法有3个參数:

    a、想要载入的layout的resource ID.
    b、载入的layout的父ViewGroup.传入container是非常重要的, 目的是为了让系统接受所要载入的layout的根view的layout參数,由它将挂靠的父view指定.
    c、布尔值指示在载入期间, 展开的layout是否应当附着到ViewGroup (第二个參数).

    2、将fragment加入到activity
            通常地, fragment为宿主activity提供UI的一部分, 被作为activity的整个viewhierarchy的一部分被嵌入. 有2种方法你能够加入一个fragment到activity layout:


    2.1、使用XML将Fragment加入到一个Activity中
           在这样的情况下。你能够像为View一样, 为fragment指定layout属性。

    <?xml version="1.0" encoding="utf-8"?>
    <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:name="com.example.news.ArticleListFragment"
                android:id="@+id/list"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
        <fragment android:name="com.example.news.ArticleReaderFragment"
                android:id="@+id/viewer"
                android:layout_weight="2"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
    </LinearLayout>
    PS

       1、<fragment> 中的 android:name属性指定了在layout中实例化的Fragment类.
         当系统创建这个activity layout时,它实例化每个在layout中指定的fragment,并调用每个上的onCreateView()方法,来获取每个 fragment的layout.系统将从fragment返回的 View直接插入到<fragment>元素所在的地方. 

       2、通过在xml中定义fragment的方式。我们不能在执行时移除fragment。假设我们想要通过切换fragments来跟用户有更好的互动。那么就须要在activity启动的时候定义fragment了。

    2.2、在执行时加入一个Fragment到Activity

    上面一节的在activity的布局文件(layout xml)中加入Fragment的方法我们已经知道了。

    如今我们将学习第二种方式。这样的方式同意我们在执行时动态的显示和隐藏fragment。为了达到在activity中动态管理Fragment,我们须要用到FragmentManager,而且通过它创建FragmentTransaction。

    activity同意移除或者替换fragment须要有例如以下条件:

      1、activity的onCreate()方法中加入初始化的fragment
      2、fragment放置位置的布局中必须有一个视图容器

    比方:res/layout/news_articles.xml文件提供了视图容器。

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    Activity中使用getSupportFragmentManager()获取FragmentManager,之后调用beginTransaction去创建一个FragmentTransaction对象, 再调用add()方法就可以加入一个fragment。 在activity中能够使用同一个FragmentTransaction对象去运行多个fragment事务,当做这样操作时。必须调用commint()方法。 以下的代码演示如何加入一个fragment到res/layout/news_articles.xml的layout:
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    
    public class MainActivity extends FragmentActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.news_articles);
    
            if (findViewById(R.id.fragment_container) != null) {
    
                if (savedInstanceState != null) {
                    return;
                }
    
                HeadlinesFragment firstFragment = new HeadlinesFragment();
    
                firstFragment.setArguments(getIntent().getExtras());
                
                // Add the fragment to the 'fragment_container' FrameLayout
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.fragment_container, firstFragment).commit();
            }
        }
    }

    add()的第一个參数是fragment要放入的ViewGroup, 由resource ID指定,第二个參数是须要加入的fragment.一旦用FragmentTransaction做了改变,为了使改变生效,必须调用commit().

    PS

    如今再来说明另外一个实例,实例图例如以下。我要在四个标签页面切换(主页。手机,配件,购物车)


    代码例如以下,详细就是通过影藏和显示fragment来实现切换

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;
    import android.view.Window;
    
    public class FramentMainActivity extends FragmentActivity {
    
    	private Fragment[] fragments;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		requestWindowFeature(Window.FEATURE_NO_TITLE);
    		setContentView(R.layout.activity_frament_main);
    
    		fragments = new Fragment[4];
    		fragments[0] = getSupportFragmentManager().findFragmentById(R.id.farment_main);
    		fragments[1] = getSupportFragmentManager().findFragmentById(R.id.farment_phone);
    		fragments[2] = getSupportFragmentManager().findFragmentById(R.id.farment_accessory);
    		fragments[3] = getSupportFragmentManager().findFragmentById(R.id.farment_cart);
    		getSupportFragmentManager().beginTransaction().
    		hide(fragments[1]).hide(fragments[2]).hide(fragments[3]).show(fragments[0]).commit();
    
    	}
    
    	public void mainClick(View view){
    		getSupportFragmentManager().beginTransaction().hide(fragments[1]).hide(fragments[2]).hide(fragments[3]).show(fragments[0]).commit();
    	}
    	public void phoneClick(View view){
    		getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[2]).hide(fragments[3]).show(fragments[1]).commit();
    	}
    	public void accessoryClick(View view){
    		getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[3]).show(fragments[2]).commit();
    	}
    	public void cartClick(View view){
    		getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2]).show(fragments[3]).commit();
    	}
    }
    

    3、Frament 管理

    要管理 fragment,需使用 FragmentManager,要获取它,需在 activity 中调用方法 getFragmentManager()。

    能够用 FragmentManager 来做以上事情:

    用法 findFragmentById()或 findFragmentByTag(),获取 activity 中已存在的 fragment

    用法 popBackStack()从 activity 的后退栈中弹出 fragment(这能够模拟后退键引发的动作)

    用方法 addOnBackStackChangedListerner()注冊一个侦听器以监视后退栈的变化

    还能够使用 FragmentManager 打开一个 FragmentTransaction 来运行 fragment 的事务,比方加入或删除 fragment。

    在 activity 中使用 fragment 的一个伟大的优点是能跟据用户的输入对 fragment 进行加入、删除、替换以及运行 其他动作的能力。

    提交的一组 fragment 的变化叫做一个事务。

    事务通过 FragmentTransaction 来运行。还能够把每一个 事务保存在 activity 的后退栈中,这样就能够让用户在 fragment 变化之间导航(跟在 activity 之间导航一样)。

    能够通过 FragmentManager 来取得 FragmentTransaction 的实例,例如以下:

    FragmentManagerfragmentManager = getFragmentManager();
    FragmentTransactionfragmentTransaction =fragmentManager.beginTransaction(); 

    一个事务是在同一时刻运行的一组动作(非常像数据库中的事务)。

    能够用 add(),remove(),replace()等方法构成事务,最后使用 commit()方法提交事务。在调用 commint()之前,能够用addToBackStack()把事务加入到一个后退栈中, 这个后退栈属于所在的 activity。有了它,就能够在用户按下返回键时,返回到 fragment 运行事务之前的状态。如 下例:演示了怎样用一个 fragment 取代还有一个 fragment,同一时候在后退栈中保存被取代的 fragment 的状态。

    4、为Activity创建事件回调方法

    在一些情况下, 你可能须要一个fragment与activity分享事件. 一个好的方法是在fragment中定义一个回调的interface, 并要求宿主activity实现它.当activity通过interface接收到一个回调, 必要时它能够和在layout中的其它fragment分享信息.
        比如, 假设一个新的应用在activity中有2个fragment – 一个用来显示文章列表(framgent A), 还有一个显示文章内容(fragment B) – 然后 framgent A必须告诉activity何时一个list item被选中,然后它能够告诉fragmentB去显示文章.

    PS

    最后在简单说说一个项目的大致实现,比方在手机上面实现了一个FragmentActivity + 多个fragment(登录。菜单,具体,账户等页面)。

    1、每个项目包含非常多活动,每个活动(FragmentActivity)相互不影响,每个活动(FragmentActivity)包含非常多子活动(fragment,一个页面),每个子活动也相互不影响.

    2、每个活动(FragmentActivity)用FrameLayout来显示子活动,而且对活动进行堆栈管理,实现数据不用反复拉取。就跟搜狐新闻一样的效果.

    3、登录(FragmentActivity):logo页面,登录页面

    4、菜单(FragmentActivity):菜单选择页面(側边栏,滑动),子菜单功能(每个新闻页面)

    5、其它(FragmentActivity):上传页面,下载页面等

    每个活动(FragmentActivity)实现了相互不影响。



  • 相关阅读:
    hive数据仓库入门到实战及面试
    clickhouse入门到实战及面试
    透过源码看懂Flink核心框架的执行流程
    hive经典练习题
    flink批处理从0到1学习
    flink基础篇
    flink进阶篇
    flink源码阅读
    spark优化总结
    uni-app跨平台移动应用开发
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5393911.html
Copyright © 2011-2022 走看看