LoginSignup
2
2

More than 5 years have passed since last update.

Titanium で View が端末に表示されたときになにか処理を行う方法

Last updated at Posted at 2014-07-01

View が表示されてから実施したい処理がありました.
(例えば,該当Viewの高さの値を使った処理などは,表示されてからじゃないと処理できないですね!)

postlayout イベントに処理を愚直に書いたら大変なことになった

postlayout という名前と,ドキュメントの1行目だけ読んでなんとなくいけそーだなーと思って,alertでも仕込んでみたら大変なことになりました.(Nexus5 が再起動しました)

postlayout についてのドキュメントの記載は...

Fired when a layout cycle is finished.

This event is fired when the view and its ancestors have been laid out. The rect and size values should be usable when this event is fired.

This event is typically triggered by either changing layout properties or by changing the orientation of the device. Note that changing the layout of child views or ancestors can also trigger a relayout of this view. On Mobile Web, this event can also be triggered by resizing the browser window.

Note that altering any properties that affect layout from the postlayout callback may result in an endless loop.

よく読むと見た目が変わるたびに起動するようでかなり呼ばれるんですね.実質的無限ループです.

ちょっと工夫して解決

一度呼ばれたらイベントリスナーの処理を消せばいい!

$.hogehoge.addEventListener('postlayout', function(){
    // なにかの処理

    // postlayout のリスナーを全部消す
    $.hogehoge.removeEventListener('postlayout', arguments.callee);
});

これでなんとかやれそうでした.

参考文献

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2