检测何时绘制片段? [英] Detect when a fragment is drawn?

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

问题描述

基本上,我需要 onWindowFocusChanged()的功能,但需要在 Fragment 上.不幸的是, onWindowFocusChanged()侦听器实际上并不可用于片段,因此我不确定该怎么做.

Basically I need the functionality of onWindowFocusChanged() but on a Fragment. The onWindowFocusChanged() listener isn't actually available to fragments unfortunately, so I'm not sure what to do.

有什么办法吗?

推荐答案

片段是通过您在 onCreateView()中返回的 View 绘制"的.您可以在其中使用事件处理程序.通常,如果在第一次查看 View 时需要运行一个命令,则可以在 Runnable 中分配它并将其附加到 View 通过 post()方法.

Fragments are "drawn" via the View you return in onCreateView(). You can use the event handlers in that. Usually, if there's a command that needs to be run when the View first comes to view, you can assign it in a Runnable and attach it to the View via the post() method.

getView().post(new Runnable() {
    @Override
    public void run() {
      // code you want to run when view is visible for the first time
    }
  }
)

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

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