我认为有两种方法可行:
A。与您拥有的 Activity 通信并通过该拥有的 Activity 将消息转发到其他 Fragment,详细信息可以在此处的官方 android 文档中找到:
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
引用:
在某些情况下,您可能需要一个片段来与
活动。一个很好的方法是定义一个回调接口
在片段内部并要求宿主活动实现它。
当activity通过接口接收到回调时,它可以
根据需要与布局中的其他片段共享信息。
通信接口可能如下所示:
public interface IActionListener{
//You can also add parameters to pass along etc
public void doSomething();
}
片段看起来像这样:
public class MyFragment extends Fragment{
private WeakReference<IActionListener> actionCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
actionCallback = new WeakReference<IActionListener>((IActionListener) activity);
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement IActionListener.");
}
}
}
我在这里使用 WeakReference,但这完全取决于您。您现在可以使用 actionCallback 与拥有的 Activity 进行通信并调用 IActionListener 中定义的方法。
拥有的 Activity 如下所示:
public class MyActivity extends ActionBarActivity implements IActionListener {
public void doSomething(){ //Here you can forward information to other fragments }
}
B。现在至于 第二种方法 - 您也可以让片段直接使用接口相互通信 - 这样您就不必知道您正在与之交谈的片段的确切类,这可以确保松耦合。
设置如下:您有两个(或更多)片段和一个活动(启动第二个片段)。我们有一个接口,它可以让 Fragment 2 在完成任务后向 Fragment 1 发送响应。为了简单起见,我们只是重用了我在 A.
中定义的接口
这是我们的片段 1:
public class FragmentOne extends Fragment implements IActionListener {
public void doSomething() {//The response from Fragment 2 will be processed here}
}
使用 A. Fragment 1 中描述的方法要求其拥有的 Activity 启动 Fragment 2。但是,Activity 会将 Fragment 1 作为参数传递给 Fragment 2,因此 Fragment 2 稍后可以间接访问 Fragment 1 并发送回复.让我们看看 Activity 是如何准备 Fragment 2 的:
public class MyActivity extends ActionBarActivity {
// The number is pretty random, we just need a request code to identify our request later
public final int REQUEST_CODE = 10;
//We use this to identify a fragment by tag
public final String FRAGMENT_TAG = "MyFragmentTag";
@Override
public void onStartFragmentTwo() {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
// The requesting fragment (you must have originally added Fragment 1 using
//this Tag !)
Fragment requester = manager.findFragmentByTag(FRAGMENT_TAG);
// Prepare the target fragment
Fragment target = new FragmentTwo();
//Here we set the Fragment 1 as the target fragment of Fragment 2's
//communication endeavors
target.getSelf().setTargetFragment(requester, REQUEST_CODE);
// Hide the requesting fragment, so we can go fullscreen on the target
transaction.hide(requester);
transaction.add(R.id.fragment_container, target.getSelf(), FRAGMENT_TAG);
transaction.addToBackStack(null);
transaction.commit();
}
}
提示:我正在使用 Support-Framework,所以如果您只为 > Android 3.0 开发,您可以简单地使用 FragmentActivity 而不是 ActionBarActivity。
现在 FragmentTwo 正在启动,让我们看看 FragmentTwo 将如何与 FragmentOne 通信:
public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(savedInstanceState != null){
// Restore our target fragment that we previously saved in onSaveInstanceState
setTargetFragment(getActivity().getSupportFragmentManager().getFragment(savedInstanceState, TAG),
MyActivity.REQUEST_CODE);
}
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Retain our callback fragment, the TAG is just a key by which we later access the fragment
getActivity().getSupportFragmentManager().putFragment(outState, TAG, getTargetFragment());
}
public void onSave(){
//This gets called, when the fragment has done all its work and is ready to send the reply to Fragment 1
IActionListener callback = (IActionListener) getTargetFragment();
callback.doSomething();
}
}
现在将调用 Fragment 1 中的 doSomething() 实现。