quot;
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>
// 必须使用 FragmentTransaction 中的 API
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 使用 add() 方法添加一个片段,指定要添加的片段以及将其插入哪个视图
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
// 调用 commit() 以使更改生效
fragmentTransaction.commit();
3. 管理fragment:FragmentManager
FragmentManager的执行操作包括:
- 通过
findFragmentById()
(对于在Activity
布局中提供 UI
的片段)或 findFragmentByTag()
(对于提供或不提供 UI 的片段)获取 Activity
中存在的片段。
- 通过
popBackStack()
(模拟用户发出的返回命令)将片段从返回栈中弹出。
- 通过
addOnBackStackChangedListener()
注册一个侦听返回栈变化的侦听器。
3.1. 执行片段事务
- Activity 中使用片段的一大优点是,可以根据用户行为通过它们执行添加、移除、替换以及其他操作。也称为事务
- 可以将每个事务保存到由 Activity 管理的返回栈内,从而让用户能够回退片段更改(类似于回退 Activity)。
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack 在返回栈中保留先前状态
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
3.2. 与Activity通信
- 片段可以通过
getActivity()
访问Activity
实例,并轻松地执行在Activity
布局中查找视图等任务。
Activity
也可以使用findFragmentById()
或 findFragmentByTag()
,通过从FragmentManager
获取对 Fragment
的引用来调用片段中的方法
// fragment - > activity
View listView = getActivity().findViewById(R.id.list);
// activity - > fragment
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
(1) 创建对Activity的事件回调
- 在片段内定义一个回调接口,并要求宿主 Activity 实现它。 当 Activity 通过该接口收到回调时,可以根据需要与布局中的其他片段共享这些信息。
- 一个新闻应用的 Activity 有两个片段 — 一个用于显示文章列表(片段 A),另一个用于显示文章(片段 B)— 那么片段 A 必须在列表项被选定后告知 Activity,以便它告知片段 B 显示该文章。
//fragment定义接口
public static class FragmentA extends ListFragment {
...
// Container Activity must implement this interface
public interface OnArticleSelectedListener {
public void onArticleSelected(Uri articleUri);
}
...
OnArticleSelectedListener mListener;
...
// 确保宿主 Activity 实现此接口
// 通过转换传递到 onAttach() 中的 Activity 来实例化 OnArticleSelectedListener 的实例
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
// 列表点击事件
OnArticleSelectedListener mListener;
...
@Override
public void onListItemClick(Li