为了让Fragment UI部件能够被重用,Fragment被定义成自包含、模块化的部件,拥有自己的布局和行为。这些Fragment被创建后,可以通过应用逻辑将其与Activity或者其他Fragment相关联从而实现一个复合UI。
当需要一个Fragment与其他Fragment交互时,如基于用户事件的内容修改。所有的Fragment之间的通信都通过其关联的Activity,而不会直接跟对方Fragment通信。
定义接口
为了让Fragment能跟Activity通信,需要为Fragment定义一个接口,并在Activity调用该接口。Fragment在onAttach()生命周期中方法中获取到接口方法,之后就可以通过调用接口中的方法与Activity进行通信。下面展示了一个Fragment与Activity的通讯实现:
public classHeadlinesFragment extendsListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interfaceOnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch(ClassCastException e){
throw newClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}
这样,Fragment就能通过定义为OnHeadlineSelectedListener接口的mCallback实例来调用onArticleSelected()(或者接口内其他方法)来向Activity传递消息。例如,下面的方法在Fragment中定义,当用户点击列表框时被调用。Fragment通过回调接口将事件传递到Activity。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
实现接口
为了能够让Activity处理Fragment的事件,必须在Activity中在Fragment定义的接口。下面的代码实现了上一小节中定义的接口:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
向Fragment传递消息
Activity通过findFragmentById()获取Fragment实例,然后直接调用Fragment的公共方法,即可向Fragment传递消息。
例如,假设前面介绍的Activity包含另一个用来显示之前定义的回调方法传递的数据的Fragment,Activity可以将从回调方法传过来的信息传递给用于显示它们的这个Fragment。
public static class MainActivity extends Activity implements HeadlinesFragment.OnHeadlineSelectedListener{ ... public void onArticleSelected(int position) { // The user selected the headline of an article from the HeadlinesFragment // Do something here to display that article ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment); if (articleFrag != null) { // If article frag is available, we're in two-pane layout... // Call a method in the ArticleFragment to update its content articleFrag.updateArticleView(position); } else { // Otherwise, we're in the one-pane layout and must swap frags... // Create fragment and give it an argument for the selected article ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } } }