请有人详细说明! 如果我们已经实现了ViewModel在两个片段之间进行通信,那么这些片段可以绕过相应的活动并直接与ViewModel进行通信吗?如果是,那么这是否是一个好习惯(为什么以及为什么不这样做)?
答案 0 :(得分:0)
您可以使用类似本文https://developer.android.com/training/basics/fragments/communicating
的界面定义接口
要允许Fragment与它的Activity通信,可以在Fragment类中定义一个接口,并在Activity中实现它。片段在其onAttach()
生命周期方法中捕获接口实现,然后可以调用Interface方法以与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");
}
}
...
}
现在,片段可以通过使用OnHeadlineSelectedListener接口的mCallback实例调用onArticleSelected()
方法(或接口中的其他方法)将消息传递给活动。
例如,当用户单击列表项时,将调用片段中的以下方法。该片段使用回调接口将事件传递给父活动。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
实现界面 为了接收来自片段的事件回调,承载它的活动必须实现片段类中定义的接口。
例如,以下活动实现了上例中的接口。
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
}
}
向片段发送邮件
主机活动可以通过使用findFragmentById()
捕获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();
}
}
}