简体   繁体   English

android实现片段中的侦听器

[英]android implement listener in fragment

I am new in Android development. 我是Android开发的新手。 I want to add a listener in my fragment which upon button click will send a message. 我想在片段中添加一个侦听器,单击该按钮将发送一条消息。 I need to listen to this in my main fragment class so that my activity can access this main fragment and process the message. 我需要在我的主要片段类中收听此内容,以便我的活动可以访问该主要片段并处理消息。 I also need to remove the listener in my main fragment. 我还需要删除主片段中的侦听器。 So how do I implement this? 那么我该如何实现呢?

EDIT 编辑

I have MyFragment 我有MyFragment

sendMessage(message) is a method in MyFragment which has a button click. sendMessage(message)MyFragment中的一个单击按钮的方法。 Need to implement listener here? 需要在这里实现监听器吗?

In MainFragment How to handle this sendMessage event here and send to MAinActivity MainFragment如何在此处处理此sendMessage事件并发送给MAinActivity

The flow is like MyFragment(childfragment) -> MAinFragment -> MainActivity 流程就像MyFragment(childfragment) -> MAinFragment -> MainActivity

In your fragment class, say MyFragment.java create an interface 在您的片段类中,说MyFragment.java创建一个接口

public interface MyFragmentInterfacer{
    void onButtonClick(String msg);
}
MyFragmentInterfacer fragmentInterfacer;

//Override this function as below to set fragmentInterfacer
@Override
public void onAttach(Context context){
    fragmentInterfacer = (MyFragmentInterfacer)context;
}

and where you want to invoke this function 以及您要在何处调用此函数

myButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        if(fragmentInterfacer != null){
             fragmentInterfacer.onButtonClick("This is my Message");
        }
    }
});

Then in your main activity where you want to get this message, implement this interface 然后在您要获取此消息的主要活动中,实现此接口

public class MainActivity implements MyFragment.MyFragmentInterfacer{
.
.
}

and then implement the interface function in MainActivity 然后在MainActivity中实现接口功能

@Override
public void onButtonClick(String msg){
    //Do something with this message
}

To remove this listener, use 要删除此侦听器,请使用

fragmentInterfacer = null;

in your fragment class when you want to disable this listener. 若要禁用此侦听器,请在片段类中使用。

Or you can remove the onClickListener from button 或者您可以从按钮中删除onClickListener

myButton.setOnClickListener(null);

or disable clicking on button 或禁用单击按钮

myButton.setClickable(false);

For communicating between your activity and fragments you can interfaces. 为了在活动和片段之间进行通信,可以进行接口。 The idea is basically to define an interface inside a given fragment and let the activity implement that interface. 这个想法基本上是在给定的片段内定义一个接口,然后让活动实现该接口。

Once it has implemented that interface, you could do anything you want in the method it overrides. 一旦实现了该接口,您就可以在其覆盖的方法中做任何您想做的事情。

To help you with some code level details, you can refer http://simpledeveloper.com/how-to-communicate-between-fragments-and-activities/ 为了帮助您了解一些代码级别的详细信息,您可以参考http://simpledeveloper.com/how-to-communicate-between-fragments-and-activities/

Hope this will help! 希望这会有所帮助!

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

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