Fragment 表示 Activity 中的行为或用户界面部分。您可以将多个Fragment组合在一个 Activity 中来构建多窗格 UI,以及在多个 Activity 中重复使用某个Fragment。
Fragment必须始终嵌入在 Activity 中,其生命周期直接受宿主 Activity 生命周期的影响。
向 Activity 添加Fragment
通常,Fragment向宿主 Activity 贡献一部分 UI,作为 Activity 总体视图层次结构的一部分嵌入到 Activity 中。可以通过两种方式向 Activity 布局添加Fragment:
- 在 Activity 的布局文件内声明Fragment
<?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.TestFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
android:name 属性指定要在布局中实例化的 Fragment 类。
- 通过编程方式将片段添加到某个现有 ViewGroup
想在Activity 中执行Fragment事务(如添加、删除或替换Fragment),必须使用 FragmentTransaction 。可以像下面这样从 Activity 获取一个 FragmentTransaction 实例:
FragmentManager fragmentManager = getFragmentManager() //getSupportFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
可以使用 add() 方法添加一个Fragment,指定要添加的Fragment以及将其插入哪个视图。
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
传递到 add() 的第一个参数是 ViewGroup,即应该放置Fragment的位置,由资源 ID 指定,第二个参数是要添加的Fragment。一旦通过 FragmentTransaction 做出了更改,就必须调用 commit() 以使更改生效。
在调用 commit() 之前,可以调用 addToBackStack()方法将事务添加到Fragment事务返回栈。 该返回栈由 Activity 管理,允许用户通过按“返回” 按钮返回上一Fragment状态。
Fragment newFragment = new ExampleFragment();
//
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
//
transaction.commit();
上例中,newFragment 会替换目前在 R.id.fragment_container ID 所标识的布局容器中的任何片段(如有)。通过调用 addToBackStack() 可将替换事务保存到返回栈,以便用户能够通过按“返回” 按钮撤消事务并回退到上一片段。
Fragment与Activity间通信
Fragment可以通过 getActivity() 访问 Activity 实例,并轻松地执行在 Activity 布局中查找视图等任务。
View listView = getActivity().findViewById(R.id.list);
Activity 也可以使用 findFragmentById() 或 findFragmentByTag(),通过从 FragmentManager 获取对 Fragment 的引用来调用片段中的方法。
ExampleFragment fragment = (ExampleFragment) getFragmentManager().
findFragmentById(R.id.example_fragment);
Fragment声明周期
Fragment必须是依存于Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。
Activity 生命周期与片段生命周期之间的最显著差异在于它们在其各自返回栈中的存储方式。 默认情况下,Activity 停止时会被放入由系统管理的 Activity 返回栈(以便用户通过“返回” 按钮回退到Activity)。不过,仅当您在删除片段的事务执行期间通过调用 addToBackStack() 显式请求保存实例时,系统才会将片段放入由宿主 Activity 管理的返回栈。
创建Fragment
要想创建片段,必须创建 Fragment 的子类或者它的子类(如DialogFragment,ListFragment,PreferenceFragment)的子类。
通常,至少应实现以下生命周期方法:
- onCreate():应该在方法内内初始化想在fragment暂停或停止后恢复时保留的必需片段组件。
- onCreateView():系统会在Fragment 首次绘制其用户界面时调用此方法。 要想为Fragment 绘制 UI,从此方法中返回的 View 必须是Fragment 布局的根视图。如果Fragment 未提供 UI,可以返回 null。
- onPause():通常应该在此方法内确认在当前用户会话结束后仍然有效的任何更改(因为用户可能不会返回)。
下面是创建的一个Fragment
public class SecondFragment extends Fragment {
private static final String TEXT = "text";
private String mStr;
public SecondFragment() {
}
//在其他地方想要初始化fragment不推荐直接用new Fragment()
//推荐使用一下方式获得fragment
public static SecondFragment newInstance(String text) {
SecondFragment fragment = new SecondFragment();
Bundle args = new Bundle();
args.putString(TEXT, text);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreate()");
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mStr = getArguments().getString(TEXT);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
savedInstanceState) {
Log.i(TAG, "onCreateView()");
View v = inflater.inflate(R.layout.fragment_second, container, false);
TextView textView = (TextView)v.findViewById(R.id.second_text);
textView.setText(mStr);
return v;
}
}
fragment的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/second_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="25sp"/>
</RelativeLayout>