第七次作业

什么是Fragment? 

由于Android是一款开源的系统,在国内外不同的厂商研发出了很多不同的机型,甚至将Android应用到了平板中,但是这意味着不同厂商出产的手机、平板的屏幕大小不一,例如,组件排版安装5.0寸手机制作的程序,如果在4.7寸的手机上,视觉效果体验虽说会降低,但也不至于不可接受。但是如果放到6.0寸的手机,乃至平板上,用户的视觉体验会大幅度降低。 
这就导致了Android研发的工作中有比较大的比重的分辨率匹配,而Fragment则是用来解决这一问题的。 
这是因为,Fragment是依赖于Activity的,在Activity的布局中创建一个FrameLayout组件,就可以在上面根据不同的需要显示很多个Fragment(当然不能同时显示)。并且Fragment有自己的生命周期、自己的布局文件、独立的事件处理,极端的说,你甚至可以把一个Fragment当做一个Activity来使用。

Fragment的基本原理

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_gravity="center_vertical"
        android:text="这里是MainActivity"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/ButtonShowFragmentNow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示FragmentNow"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/ButtonShowFragmentTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示FragmentTwo"
            android:textAllCaps="false" />
    </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">

        <Button
            android:id="@+id/ButtonAmendFragmentTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改FragmentTwo"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/ButtonReplaceFragmentNow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="替换" />

        <Button
            android:id="@+id/ButtonDeleteFragmentNow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除FragmentNow"
            android:textAllCaps="false" />
    </LinearLayout>

    <TextView
        android:id="@+id/TextView"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="5dp"
        android:text="这就是用来被Fragment修改的"
        android:textAllCaps="false"
        android:textSize="18sp" />

    <FrameLayout
        android:id="@+id/FrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#D02090" />

</LinearLayout>

1、低版本API对Fragment的支持

Fragment必须被加载进Acitivity中,才能呈现。而在低于3.0版本的API中,由于不存在Fragment,因此必须使用support包:

(1)对于1.6(API=4)及以上版本:

 创建Fragment时,应该继承android.support.v4.app.Fragment;

 创建Activity时,应该继承android.support.v4.app.FragmentActivity;

(2)对于2.1(API=7)及以上版本:(为同时支撑ActionBar)

创建Fragment时,应该继承android.support.v4.app.Fragment;(这个还需要确认一下)

创建Activity时,应该继承android.support.v7.app.ActionBarActivity

(3)他们之间的关系为:

java.lang.Object
   ↳ android.content.Context
    ↳ android.content.ContextWrapper
    ↳android.view.ContextThemeWrapper
    ↳android.app.Activity
    ↳android.support.v4.app.FragmentActivity
    ↳android.support.v7.app.ActionBarActivity


2、addToBackStack()

Keep in mind that when you perform fragment transactions, such as replace or remove one, it's often appropriate to allow the user to navigate backward and "undo" the change. To allow the user to navigate backward through the fragment transactions, you must call addToBackStack() before you commit the FragmentTransaction.

3、Fragment之间的通信
To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity.

原文地址:https://www.cnblogs.com/minmeishaonv/p/11730380.html