您的位置:首页 > 移动开发 > Android开发

Fragment间通信

2016-11-01 21:49 288 查看
为了重用Fragment UI组件,您应该将每个组件构建为一个完全自包含的模块化组件,以定义其自己的布局和行为。一旦定义了这些可重用的Fragment,您可以将它们与一个Activity相关联,并将它们与应用程序逻辑连接,以实现整个复合UI。

通常,您将需要一个Fragment与另一个Fragment进行通信,例如根据用户事件更改内容。所有Fragment到Fragment的通信通过相关联的Activity完成。两个Fragment永远不应该直接通信。

定义接口

要允许Fragment与其Activity进行通信,您可以在Fragment类中定义一个接口,并在Activity中实现它。 Fragment在其
onAttach()
生命周期方法期间捕获接口实现,然后可以调用Interface方法以与Activity进行通信。

这里是Fragment to Activity通信的示例:

public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;

// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
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 new ClassCastException(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);
}


实现接口

为了从Fragment接收​​事件回调,托管它的Activity必须实现Fragment类中定义的接口。

例如,以下Activity实现了上述示例中的接口。

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息