Kotlin android Extension: Say Goodbye to Findviewbyid (KAD 04)

Source: Internet
Author: User

Antonio Leiva

Time:Dec

Original link:http://antonioleiva.com/kotlin-android-extensions/

You may be tired of using Findviewbyid to write Android views day after day. Or you might abandon it instead to use the famous Butterknife library. Then you will love Kotlin's Android extension.

Kotlin's Android Extension

Kotlin's Android extension is one of the regular plugins of the Kotlin plugin, which seamlessly covers the activities view, fragments y view.

Let's see how simple it is.

Integrate Kotlin's Android extensions into our code

Although you can integrate it into your code when you want to use a plugin, you still need to add additional apply to the Android module:

1 Apply plugin: ' Com.android.application '2 Apply plugin: ' Kotlin-android '3 Apply plugin: ' Kotlin-android-extensions '

These are all you need to add. So you're ready to use it.

overriding views in activity or fragment

In this case, overriding the view in your activity or fragment is as convenient as defining the view ID directly in the XML.

Imagine that you have XML like this:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Framelayout3     xmlns:android= "Http://schemas.android.com/apk/res/android"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent">6  7     <TextView8         Android:id= "@+id/welcomemessage"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One android:layout_gravity= "Center" A Android:text= "Hello world!"/> -   - </Framelayout>

As you can see, TestView has a welcomemessage ID.

Just write it in your mainactivity:

1 override fun OnCreate (savedinstancestate:bundle?) {2     Super. OnCreate (savedinstancestate)3    Setcontentview ( R.layout.activity_main)4  5     welcomemessage.text = "Hello kotlin!" 6 }

In order to be able to use it, you need a special import (which I write below), and the IDE is able to automatically add the import it. This is not very easy!

Import kotlinx.android.synthetic.main.activity_main.*

Plug-in generation code can store the view cache (caches) so that when you revisit the view again, you don't need another findviewbyid.

Overwrite other views by one view

We have this view:

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 android:orientation= "vertical"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent">5     6     <ImageView7         Android:id= "@+id/itemimage"8 Android:layout_width= "Match_parent"9 Android:layout_height= "200DP"/>Ten      One     <TextView A         Android:id= "@+id/itemtitle" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content"/> the   - </LinearLayout>

If you add adapter in it.

You can access the sub-view directly with this plugin:

1 val itemview = ... 2 ItemView.itemImage.setImageResource (R.mipmap.ic_launcher) 3 itemView.itemTitle.text = "My text"

Although the plugin also helps you fill in the import, this type is a little different:

Import kotlinx.android.synthetic.main.view_item.view.*

There are a few things you need to know about this:

    1. At compile time, you can reference any view from any other view. This means that you can refer to any view from one view, not necessarily its child view. However, at run time this will fail because the view it is trying to overwrite does not exist.
    2. In this case, the views are not cached for activities and fragments .

However, you just have to use it carefully and it's a very useful tool.

Conclusion

you already know how to Kotlin easy to handle in Android view. With a simple plugin, we can ignore all the bad code that involves view recovery after the extension. This plugin will produce the correct type without any problems according to our requirements feature.

Kotlin android Extension: Say Goodbye to Findviewbyid (KAD 04)

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.