Android Fragment

在一个容器中动态加载Fragment

  • 在碎片宿主活动的布局中创建一个ViewGroup加载碎片,而不用fragment标签静态加载碎片
  • 使用FragmentManager管理碎片
  • 使用FragmentTransaction操作碎片
    • 可以添加、删除、显示、隐藏碎片
  • FragmentTransaction操作碎片需要考虑是否要保存状态,并且可以按返回键回退到上一个碎片
    • FragmentTransaction对象的replace方法操作会先删除容器里面的碎片,然后再添加其它碎片,所以我们可以使用在commit之前保存装备删除的碎片,使用addToBackStack(null)添加到返回栈中,从而使得碎片的周期走第二条路线。
    • FragmentTransaction对象的replace方法操作的时候最好能添加tag,方便FragmentManager对象使用findFragmentByTag找到碎片对象。

案例

fragment_f1.xml碎片布局文件


<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="match_parent"
        android:text="F1" />

</LinearLayout>

fragment_f2.xml碎片布局文件


<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="match_parent"
        android:text="F2" />


</LinearLayout>

F1碎片java文件


package com.hzj163.myfragmentdynamic;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */


public class F1 extends Fragment {

    View view;

    public F1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (view == null) {
            view = inflater.inflate(R.layout.fragment_f1, container, false);
        }
        Log.i("hzj", "f1:onCreateView");
        return view;

    }
}

F2碎片java文件


package com.hzj163.myfragmentdynamic;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */


public class F2 extends Fragment {

    View view;

    public F2() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (view == null) {
            view = inflater.inflate(R.layout.fragment_f2, container, false);
        }
        Log.i("hzj", "f2:onCreateView");
        return view;

    }
}

activity_main.xml活动布局文件


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <!--加载碎片的ViewGroup-->
    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="btn1"
            android:text="F1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="btn2"
            android:text="F2" />
    </LinearLayout>


</LinearLayout>

MainActivity活动java文件


package com.hzj163.myfragmentdynamic;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    F1 f1;
    F2 f2;
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        f1 = new F1();
        f2 = new F2();
        fragmentManager = getSupportFragmentManager();
        openFragment(f1, "f1");
    }

    /**
     * @param fragment 碎片对象
     * @param tag      该碎片的标签
     */
    private void openFragment(Fragment fragment, String tag) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.linear1, fragment, tag);
        //添加到返回按键堆栈,这样就可以执行碎片的第二条周期,也就是不会每次replace的时候都去执行onAttach
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    public void btn1(View view) {
        openFragment(f1, "f1");
    }

    public void btn2(View view) {
        openFragment(f2, "f2");
    }
}