Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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);
});

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

参考文献

http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.View

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away