Customizing Android View with Kotlin (KAD 06)

Source: Internet
Author: User

Antonio Leiva

Time:Dec,

Original link:https://antonioleiva.com/custom-views-android-kotlin/

As we read about class articles, you may remember that typically only one constructor is used. This is an issue for creating custom views.

The Android framework wants to have multiple constructors that correspond to different create view locations and create view methods (through code, using XML, setting up themes, and so on) so that we cannot ignore this situation.

To do this, the Kotlin team provides the ability to have multiple constructors in the same class, as described here.

Compatibility with Java is a fundamental requirement for kotlin, so no matter what you encounter, you should expect to have a way to achieve your needs.

Create a custom view in Kotlin

even if you already have experience creating custom views and some Kotlin knowledge, or perhaps for the first time in Kotlin Create a custom view in, you'll find it a little more complicated.

in the Kotlin The implementation of several constructors is one of the most complex tasks, and to be precise, this is a relatively rare usage.

But don't worry, once you touch it, the rest is very similar.

Note: Although this article can effectively help you understand how to use the Kotlin class with multiple constructors, however, Kirillrakhman The method mentioned in the review is a better approach. Read at the end.

to create a class that inherits view

to do this, create a class as we saw earlier. For example, it inherits the View, but does not describe any constructors:

1 class Kotlinview:view {2 }

Because this code invites the constructor of the parent class to be called, the ground cannot be compiled.

For example, if you are only in Kotlin extending your view in the code, you might use the only constructor form we've seen:

1 class Kotlinview (Context:context?): View (context) {2 }

but be careful, because if you decide to add in this view XML , it will fail.

Description: You see in Context question mark on the right? In Kotlin , if we want a variable or argument to be null, we must explicitly indicate it with a question mark. The compiler will then ask us to check that it cannot be null before using the variable . Read this in the next article.

implementing multi-constructor functions

constructor with reserved words Constructor , it can be another constructor of its kind (with This ) or a parent class (with Super ).

This is what you define Android Constructors for Views:

1 classKotlinview:view {2  3Constructor (Context:context?): This(Context,NULL)4Constructor (Context:context, Attrs:attributeset?): This(context, Attrs, 0)5  6Constructor (Context:context, Attrs:attributeset, Defstyleattr:int):Super(context, attrs, defstyleattr) {7         ...8     }9  Ten}

more simple Implementation method

in the comments, Kirill mention (thank you very much!) Another method is simpler and easier to read. It is based on assigning default values to the arguments of the constructor, but requires a little tweaking.

The problem is that when you create a constructor (or any function) with the default value of the argument, the Kotlinzhong The resulting bytecode allows only those default values to be used. If You use constructors in Java, you can force the values of all arguments to be described.

this is because Java There is no such language feature. In Java , you need to solve it by generating a function overload based on your needs.

in the Kotlin , you can use the @JvmOverloads comments are generated automatically by the code.

The code looks like this:

1 class Kotlinview @JvmOverloads Constructor (2         null, Defstyleattr:int = 03 ): View (Context, attrs, defstyleattr)

Conclusion

Once you understand it, it's not complicated to produce a custom view with multiple constructors, @JvmOverloads comments are easier.

This is useful for any class that requires more than one constructor. But in general, if you can assign the default value of a parameter (thereby avoiding overloading), you usually don't need more than one constructor.

If you want to learn more about this knowledge, write the actual APP , I suggest you read my book " Android Developer's Kotlin ".

in the Kotlin Create a custom view in

Customizing Android View with Kotlin (KAD 06)

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.