如何让onWindowFocusChanged的片段? [英] how to get the onWindowFocusChanged on Fragment?

查看:631
本文介绍了如何让onWindowFocusChanged的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Android的滑动菜单采用抽屉式导航。 我知道, onWindowFocusChanged 上MainActivity工作。 我怎么能检查是hasFocus的片段?

有人说我可以通过 hasFocus 来的片段,但我不知道如何做到这一点。任何人都可以给我一些样品code?

我要↓这对我的片段来运行

  @覆盖
公共无效onWindowFocusChanged(布尔hasFocus){
    super.onWindowFocusChanged(hasFocus);

    如果(hasFocus){
        //我需要做someing。
        方法();
    }
}
 

解决方案

您可以创建一个接口,所有的片段实现了这个接口,并在你的 onWindowFocusChanged 你当前片段,并通过调用由接口提供的方法

有关片段的样本接口可以是:

 公共接口IOnFocusListenable {
   公共无效onWindowFocusChanged(布尔hasFocus);
}
 

您片段要实现这个接口:

 公共类MyFragment实现IOnFocusListenable {
    ....
    公共无效onWindowFocusChanged(布尔hasFocus){
        ...
    }
}
 

而在 onWindowFocusChanged 的活动,你可以再做 如下:

 公共类MyActivity扩展AppCompatActivity {
   @覆盖
   公共无效onWindowFocusChanged(布尔hasFocus){
        super.onWindowFocusChanged(hasFocus);

        如果(currentFragment的instanceof IOnFocusListenable){
            ((IOnFocusListenable)currentFragment).onWindowFocusChanged(hasFocus);
        }
    }
}
 


或者你创建一个监听器和活性片段加入到听众。因此,如果该片段可见你订阅了这个监听器,并且每次的 onWindowFocusChanged 事件被称为调用这个监听器。

此方法非常类似于上述,不同的是有 IOnFocusListenable 的和那些在活动触发 onWindowFocusChanged的列表方法

I am using Android Sliding Menu using Navigation Drawer. I know that onWindowFocusChanged work on MainActivity. How can I check is it hasFocus on Fragment?

someone said that I can pass the hasFocus to fragment, but I dont know how to do this. Can anyone give me some sample code?

I want to run ↓ this on my fragment

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus) {
        //I need to do someing.
        method();
    }
}

解决方案

You can either create an interface and all your fragments implement this interface, and inside your onWindowFocusChanged you get the current fragment and pass call the method provided by the interface.

A sample interface for the fragments could be:

public interface IOnFocusListenable {
   public void onWindowFocusChanged(boolean hasFocus);
}

Your fragments have to implement this interface:

public class MyFragment implement IOnFocusListenable {
    ....
    public void onWindowFocusChanged(boolean hasFocus) {
        ...
    }
}

And in the onWindowFocusChanged of your Activity you can then do the following:

public class MyActivity extends AppCompatActivity {
   @Override
   public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if(currentFragment instanceOf IOnFocusListenable) {
            ((IOnFocusListenable) currentFragment).onWindowFocusChanged(hasFocus);
        }
    }
}


Or you create a listener and the active fragment is added to the listener. So if the fragment is made visible you subscribe to this listener, and everytime the onWindowFocusChangedevent is called you call this listener.

This approach is very similar to the above with the difference that there is a list of IOnFocusListenable's and those are triggered in the activities onWindowFocusChanged method

这篇关于如何让onWindowFocusChanged的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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