Kotlin编程之Kotlin Android Extensions(扩展插件)

Kotlin编程开发Android运用程序的相关介绍


Kotlin Android Extensions


介绍:

Kotlin Android扩展插件可以节省findviewbyid(),实现与Data-Binding,Dagger框架的效果,不需要添加任何额外代码,也不影响任何运行时体验。

Kotlin Android扩展是Kotlin 插件的组成之一,不需要在单独安装插件。

在Gralde中配置

apply plugin: 'kotlin-android-extensions'

点击syncNow,开始同步。

这里写图片描述

导入合成属性


使用Kotlin Android Extensions在以下常用的情况:

1. 在Activity中

按照import kotlinx.android.synthetic.main.<布局>.*格式,可以导入布局文件中所有控件属性。

这里写图片描述

接着,根据控件的Id直接引用控件对象:

这里写图片描述

最终,简洁的代码如下:

class ScrollingActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scrolling)

        //等同于findViewById(R.id.toolbar) as Toolbar
        var  toolbarView=toolbar
        //为了更容易看懂,声明了一个变量。最简洁: setSupportActionBar(toolbar),一行搞定。
        setSupportActionBar(toolbarView)
        ..........
    }
    .........
}
2. 在Adapter中

在Adapter中使用movielist_item.xml布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">
    <ImageView
        android:id="@+id/movielist_item_iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"/>
    <TextView
        android:id="@+id/movielist_item_tv"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dp"
        android:textStyle="bold"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content" />
</LinearLayout>

在Adapter中,先按import kotlinx.android.synthetic.main.item布局名.view.*的方式导入。

import kotlinx.android.synthetic.main.movielist_item.view.*

internal class MovieListAdapter(var context: Context, var list: List<MovieList.Movie>) : RecyclerView.Adapter<MovieListAdapter.ViewHolder>() {

    internal class ViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView) {
        /**
         * 这里使用Kotlin Android 扩展,省略了findViewById().
         * 在最上面导入了import kotlinx.android.synthetic.main.movielist_item.view.*
         */
        var imageView = rootView.movielist_item_iv
        var title_Tv= rootView.movielist_item_tv
    }
}
3. 在Fragment中

和Adapter中使用类似

Fragment中对应的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">
     //.........
    <android.support.v7.widget.RecyclerView
        android:id="@+id/movieList_recyclverView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

在Fragment编写代码:

import kotlinx.android.synthetic.main.fragment_movielist.view.*

class MovieListFragment : Fragment(), MovieListConstract.View {
    .......
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView = inflater.inflate(R.layout.fragment_movielist, container, false)
        return rootView
    }
    .......
    /**
     * 加载数据
     */
    override fun loadMovie(list: List<MovieList.Movie>) {
        var recyclerView = rootView.movieList_recyclverView //引用控件
        recyclerView.layoutManager = LinearLayoutManager(activity)
        recyclerView.adapter = MovieListAdapter(activity, list)
    }
}

Android多渠道

安卓扩展插件现已支持安卓多渠道。具体详情,请阅读Kotlin Android Extensions原理

Kotlin Android Extensions的原理

Kotlin 安卓扩展作为 Kotlin 编译器的插件,主要有两大作用:

在每一个 KotlinActivity 中添加一个隐藏缓存函数以及一个变量。该方法非常简洁,因此不会直接对APK体积有明显增加。
使用函数调用替换每一个合成属性。
其工作原理是,当调用合成属性,在模块资源中 Kotlin Activity/Fragment 类作为接收器时,缓存函数将被调用.

具体详情,请阅读Kotlin Android Extensions原理

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值