android - Remove listener from ViewTreeObserver -
android - Remove listener from ViewTreeObserver -
i need hear exact 1 global layouting event set right scrolling positions. after searching little why calls scrollto(x,y)
seem ignored discovered these can called in meaningful way 1 time whole layout known. registering globallayoutlistener
, defer phone call scrollto()
.
the problem is, want scrolling once. figured phone call removeglobalonlayoutlistener()
stop listening. resulted in exception: illegalstateexception: viewtreeobserver not alive, phone call getviewtreeobserver() again
. thought fine, if observer not live won't fire events. unfortunately does: view scrolled every time layout somehow changes.
my current iteration of code looks this. alter create sure phone call scrolltogridpos()
takes place once? know add together local field mhasfired
inner class seems dirty hack me ...
final viewtreeobserver vto = mlayout.getviewtreeobserver(); vto.addongloballayoutlistener(new ongloballayoutlistener() { public void ongloballayout() { scrolltogridpos(getcenterpoint(), false); if (vto.isalive()) { vto.removeglobalonlayoutlistener(this); } } });
you should using mlayout.getviewtreeobserver()
in ongloballayout()
, rather trying access old one. eg.
mlayout.getviewtreeobserver().addongloballayoutlistener(new ongloballayoutlistener() { public void ongloballayout() { scrolltogridpos(getcenterpoint(), false); mlayout.getviewtreeobserver().removeglobalonlayoutlistener(this); } });
android
Comments
Post a Comment