Kotlin Secret recipe for Android (I): Ongloballayoutlistener

Source: Internet
Author: User

After the Spring Festival, the new "opening". Please continue to support the master. Thank you!

Original title: Kotlin Recipes for Android (I): Ongloballayoutlistener

Original link:http://antonioleiva.com/kotlin-ongloballayoutlistener/

Original Antonio Leiva (http://antonioleiva.com/about/)

Published in the original: 2016-03-16

Kotlin Secret recipe for Android (I): Ongloballayoutlistener

Today, a companion asked me how to use ongloballayoutlistener properly without the need for too many templates. This is a tricky issue and we need to go further into it.

What is Ongloballayoutlistener for?

This listener is suitable for any attempted viewtreeobserver, and it is often recalled for expansion and measurement views when various calculations, animations, and so on, are known about the width and height of the view.

Fortunately Kotlin provides good interoperability with Java, we are able to implement a single method interface in a very clear way-using its simulation properties and lambda expressions-to:

1 Recycler.viewTreeObserver.addOnGlobalLayoutListener {2     // Do whatever 3 }

What's the problem here? To prevent leaks, the recommended practice is to delete this listener immediately after you have finished using it. But because lambda expressions are used, lambda does not have the exact object, and we do not have a reference to the object.

The original way can still be used, but in the Kotlin directly with anonymous objects, every time a kitten died. If you still need to do something like this, you can't switch to a better language:

1 Recycler.viewTreeObserver.addOnGlobalLayoutListener (2        object: Viewtreeobserver.ongloballayoutlistener {3            override Fun Ongloballayout () {4                 Recycler.viewTreeObserver.removeOnGlobalLayoutListener (this); 5                 // Do whatever 6             }7         });

Find a better replacement method

Well, it's known that you don't do that. So what better way to do that? We were forced to use a method that looked less attractive, but it might be a good choice to hide the extension function.

Create a new function for the view to receive another function that creates and deletes the listener itself. Just like this:

1 Inline fun view.waitforlayout (crossinline F: (), Unit) = with (viewtreeobserver) {2     Addongloballayoutlistener (Object:ViewTreeObserver.OnGlobalLayoutListener {3        Override Fun Ongloballayout () {4             removeongloballayoutlistener (this)5             F ()6        }7    }) 8 }

You can now call this function to make sure that it adds and removes listeners themselves. Unless, you will never forget to delete:

12     // do whatever3 }

If you like, you can use the extended viewtreeobserver function instead of using view directly. It depends on you.

But we can still improve it.

This is something that the layout listener usually does after testing the view, so you need to wait for the width and height to be greater than 0. And you might want to do something when you call it in the view, why can't I convert the parameter function to the extension function ?

I also generics this function enables it to be used in any object that inherits view, and it can access all of its specified functions and properties from the written function.

1Inline Fun <T:View> t.aftermeasured (Crossinline f:t. ()Unit) {2 Viewtreeobserver.addongloballayoutlistener (Object:ViewTreeObserver.OnGlobalLayoutListener {3 override Fun Ongloballayout () {4                 if(Measuredwidth > 0 && measuredheight > 0) {5Viewtreeobserver.removeongloballayoutlistener ( This)6 f ()7                 }8             }9        })Ten}

This aftermeasured function is very similar to the former, but uses the view's properties and public methods directly within the lambda expression. For example, we can get the width of the recycler and set the layout based on the dynamic array it uses for columns.

1 recycler.aftermeasured {2     val columnCount = width/ columnWidth3     LayoutManager = Gridlayoutmanager (context, ColumnCount)4 }
Summarize

While running on Android, it's true that there are still some things that aren't doing so well, even if you're porting Kotlin, but you can always find options to improve readability and avoid uncertainties by hiding the uncertainties behind other structures. At the very least, you only need to write it once, while the other code is pretty!

Kotlin Secret recipe for Android (I): Ongloballayoutlistener

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.