父碎片到Child Fragment通信 [英] Parent fragment to Child Fragment communication

查看:187
本文介绍了父碎片到Child Fragment通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索并且没有找到嵌套片段与父片段通信的最佳方法。 Android文档声明:

I have been searching and haven't found the best way for nested fragments to communicate with parent fragment. The Android documentation states that:


所有片段到片段的通信都是通过关联的Activity完成的。两个片段不应该直接通信。

All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

假设我有一个Gallery应用程序,有一个 ViewPagerFragment(父) ImageFragment(子),用于在单击图像时全屏显示图像。我需要将文件的ArrayList从我的父片段传递给子片段。什么是正确的方法?

Suppose I have a Gallery app, there is a ViewPagerFragment (parent) and ImageFragment(child) that is used to show images full screen when they are clicked. I need to pass an ArrayList of Files from my parent fragment to the child fragment. What is a proper way to do it?

1)在父片段中为ArrayList创建一个getter并使用 getParentFragment()子片段中访问此方法?

1) Create a getter for the ArrayList in parent fragment and use the getParentFragment() in a child fragment to access this method?

2)实现界面并通过活动,但如果没有ID,我如何从活动中找到子片段

2) Implement an interface and go through the Activity, but how will I locate the child fragment from the Activity if it doesn't have an ID?

3)或者使用此在父片段

ChildFragment fragment =(ChildFragment)getChildFragmentManager()。findFragmentById(R.id.child_fragment_id) ;

只需在子片段中使用setter。

And just use a setter in child fragment.

但是我又如何获得childFragment的ID?

But again how do I get the ID of the childFragment?

提前谢谢大家

推荐答案

据我所知,通过活动进行碎片碎片的通信是针对由同一活动协调的兄弟碎片。

I understand that the Fragment to Fragment communication through the activity is for sibling fragments which are coordinated by the same activity.

在这种情况下,siblign片段由父片段协调,所以我会说通信应该由父片段管理。

In this case the siblign fragments are coordinated by a parent fragment, so I would say that the communication should be managed by the parent fragment.

如果你直接与然后你使用ChildFragmentManager()来完成这些片段。

If you are working directly with the fragments then you do that using the ChildFragmentManager().

如果您正在使用ViewPager,则将ChildFragmentManager传递给ViewPager,并将其传递给FragmentManager。

If you are working with a ViewPager you pass the ChildFragmentManager to the ViewPager insted of passing it the FragmentManager.

如果你必须传递一个List,我认为最简单的方法是让ChildFragment在准备接收它时通过调用一个方法来请求它。 ParentFragment。它总是可以通过getParentFragment()获得它的实现。

If you have to pass a List, I think the easiest is for the ChildFragment to request it when it is ready to receive it by calling a method on the ParentFragment. It can allways get its instace with getParentFragment().

我不确定调用getParentFrament()来获取列表的最安全的地方,但你可以尝试onCreateView (),在onActivityCreated()中,o在onResume()中。

I am not sure where is the safest place to call getParentFrament() to get the list but you can try in onCreateView() ,in onActivityCreated(), o in onResume().

要调用返回列表的方法,可以将父Fragment强制转换为已扩展的特定Fragment类,否则可以使用以下方法定义接口:返回List并使扩展父Fragment实现它。

To call the method that returns the list, you can cast the parent Fragment to the specific Fragment class you have extended, or else you can define an interface with the method that returns the List and make your extended parent Fragment implement it.

然后在子片段中,您通过接口调用父节点来调用方法并获取列表。

Then in the child Fragment, you refert to the parent by the interface to call the method and get the list.

这篇关于父碎片到Child Fragment通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆