简体   繁体   English

Android:如何将数据从父片段发送到子片段

[英]Android: How to send data from parent fragment to child fragment

So I have viewed other questions, and I have not been able to come up with a solution.所以我查看了其他问题,我一直无法提出解决方案。 So basically, I have a parent fragment and a child fragment and I need to pass an integer to the child fragment.所以基本上,我有一个父片段和一个子片段,我需要将一个整数传递给子片段。 The child fragment is defined in the XML, so I am not able to attach a bundle as far as I know.子片段是在 XML 中定义的,因此据我所知,我无法附加包。 It seems like it should be simple but I have not been able to figure it out.看起来它应该很简单,但我一直无法弄清楚。

Any help on how to do this would be great.关于如何做到这一点的任何帮助都会很棒。 Thanks!谢谢!

Simple solution is to get reference on your child fragment in your parent fragment onCreateView简单的解决方案是在您的父片段 onCreateView 中获取对您的子片段的引用

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    View view = inflater.inflate(R.layout.parent_fragment, container, false);
    YourChildFragment fragment = (YourChildFragment) getChildFragmentManager().findFragmentById(R.id.child_fragment_id);

    // pass your data
    fragment.setMyInt(42);

    return view;
}

You can use interface for this.您可以为此使用接口。 Create interface in your parent fragment and create it's object and getter-setter method.在父片段中创建接口并创建它的对象和 getter-setter 方法。

interface YourListner{
    void methodToPassData(Object data);
}

static YourListner listnerObj;

public void setListnerObj(YourListner listnerObj) {
    this.listnerObj = listnerObj;
}

Implement it in your child fragment.在您的子片段中实现它。

Then, add following code in parent fragment.然后,在父片段中添加以下代码。

ChilefragmentName yourfrag=((ChilefragmentName) getSupportFragmentManager().findFragmentById(R.id.fragmentId))
if(yourfrag !=null)
    {
        //Set listner
      yourfrag.setListnerObj((ChilefragmentName)yourfrag);

    }
if(listnerObj!=null)
    {
        //Pass your data
        listnerObj.methodToPassData(data);

    }

You can handle that data in implemented method in your child fragmen.您可以在子片段中的已实现方法中处理该数据。

If you have some parameters to pass to fragment which is defined through XML, I think better and simple option is to add it dynamically.如果您有一些参数要传递给通过 XML 定义的片段,我认为更好和更简单的选择是动态添加它。 In that case you can pass two params, param1 and param2 to the child fragment like this:在这种情况下,您可以像这样将两个参数 param1 和 param2 传递给子片段:

public static YourFragment newInstance(String param1, String param2) {
        YourFragment fragment = new YourFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public YourFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

The newInstance() method stores the parameters in the arguments bundle of the bundle. newInstance() 方法将参数存储在包的参数包中。 It will be useful when Android recreates the fragment.当 Android 重新创建片段时,它会很有用。 In that case, the parameters are retrieved in the onCreate() method.在这种情况下,将在onCreate()方法中检索参数。

You can use arguments to pass extra data to fragments.您可以使用参数将额外数据传递给片段。 This method works in general, not only with XML params.此方法一般适用,不仅适用于 XML 参数。

...

ChildFragment cf = new ChildFragment();

Bundle b = new Bundle();
b.putInt("myInt", 0);

MyData data = new MyData();
b.putSerializable("myObject", data); // Pass entire objects

cf.setArguments(b);

...

transaction.commit();

You can than obtain the data with the getArguments interface您可以使用getArguments接口获取数据

...
MyData data = (MyData) getArguments.getSerializable('myObject');
...

Although strictly speaking it's not answering the question, in some cases this works as well, when we actually want to pass the same arguments to the child-fragment:虽然严格来说它没有回答问题,但在某些情况下,当我们实际上想要将相同的参数传递给子片段时,这也有效:

class ChildFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle arguments = getArguments();
        if (arguments == null) {
            arguments = getParentFragment().getArguments();
        }
        if (arguments != null) {
            if (arguments.containsKey(Constants.EXTRA_FOO)) {
                // use it
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM