简体   繁体   English

如何从另一个片段调用方法

[英]How to call method from another fragment

Good day everyone. 今天是个好日子。 I have checkedListener in fragment and i need to type in this listener some method, but data which need to use i have in another fragment. 我在片段中检查了Listener,我需要在此侦听器中键入一些方法,但是需要使用的数据在另一个片段中。 How to send this data from different fragments? 如何从不同的片段发送数据? Can i call checkedListener from another fragment and type some rule for him in mainActivity class ? 我可以从另一个片段中调用checkedListener并在mainActivity类中为他键入一些规则吗? I will be very grateful for help. 我将非常感谢您的帮助。 Thank you. 谢谢。

How example i send sreeshot that i need to create (first square on the top this is mainFragment which have data of the Sum -1+4 and in bottom with diamonts i have another fragment which have checkedListener and i need to compare sum from different fragments and if true i need to setTextColor in text): 我如何发送需要创建的sreeshot的示例(顶部的第一个正方形是mainFragment,其具有Sum -1 + 4的数据,底部具有diamonts,我具有另一个具有CheckedListener的片段,我需要比较不同片段的总和如果为true,则需要在文本中设置setTextColor): 在此处输入图片说明

as @Juan answer suggests fragments should not communicate directly. 正如@Juan的答案表明,片段不应直接通信。 this is how you manage to do so. 这就是您设法做到的。

this is your activity class that implements the interface defined in fragment 这是您的活动类,实现了片段中定义的接口

public class MainActivity extends Activity implements FragmentInterface {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void fragmentInterfaceMethod(int your_value) {
        //use your_value to send it to another fragment
    }

}

your fragment class: 您的片段类:

public class MyFragment extends Fragment {

   public interface FragmentInterface {
       void fragmentInterfaceMethod(int your_value);
   }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_word_definition, container, false);

        int myValue = 123;
        ((FragmentInterface)getActivity()).fragmentInterfaceMethod(myValue);

        return mView;
    }
}

Fragments are not intended to talk directly between each other. 片段不打算彼此直接对话。 You can do one of two things: 您可以执行以下两项操作之一:

1 - Store the data in an activity common to the two fragments and have each fragment get and set the data throught the activity. 1-将数据存储在两个片段共有的活动中,并使每个片段在活动中获取并设置数据。

or 要么

2 - If the data is really bound to one of the fragments, then have the other fragment ask the activity to provide the data. 2-如果数据确实绑定到其中一个片段,则让另一个片段要求活动提供数据。 And in turn the activity can ask the framgment that has the data to hand it over so it can pass it to the requesting frament. 然后,活动可以要求具有数据的片段将其移交,以便可以将其传递给请求的框架。

The communication between fragment and activity can be done with an interface or by directly casting the activity to its concrete class. 片段和活动之间的通信可以通过接口完成,也可以直接将活动强制转换为具体的类。

And in the other direction by using a reference to the corresponding fragment. 并在另一个方向上通过使用对相应片段的引用。

Note: if you use a ViewPager for your fragments, getting the current fragment instance needs some extra steps. 注意:如果您对片段使用ViewPager,则获取当前片段实例需要一些额外的步骤。

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

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