使用Kotlin开发Android 扩展函数(Extensions)
2017-05-21 10:03
387 查看
在上一篇文章:使用Kotlin开发Android 创建工程与配置 主要是Kotlin相关的配置,这一篇主要是Kotlin Android Extensions
喜欢用简书的朋友,可以浏览简书下的博客:使用Kotlin开发Android 扩展函数(Extensions)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/27/0e98914f6368282e2f04df36783af647)
Being libraries dependent on runtime, they require annotating fields for each View
也就是说类似ButterKnife这类库,在运行时,需要对每一个控件进行注解。而对于Kotlin, 官方是这样介绍
The Kotlin Android Extensions plugin allows us to obtain the same experience we have with some of these libraries, without having to add any extra code or shipping any additional runtime.
Kotlin提供类似于这些库的相同体验,但不需要额外的代码和运行时间。我自己理解为减少findViewById的使用的同时,没有产生额外的代码,也不用在运行时对每个控件进行注解。
注意要导包import kotlinx.android.synthetic.main.activity_main.*,一般情况下Android Studio会自动添加这一句,格式一般为
import kotlinx.android.synthetic.main..*
为控件所在的布局文件,不同于Java ,在声明一个变量时,需要在变量前说明类型,Kotlin 一般使用val 和 var, var声明的变量是可变的,val 声明的变量是不可变的,同时也省略new关键字。运行如图
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/27/6883e769871f6cb78e4d3ba2a74465f1)
在Adapter中的用法也类似,在布局文件中给控件添加ID,
![](http://upload-images.jianshu.io/upload_images/3030704-8873bf18f558f7ab.png<br/>4000<br/>?imageMogr2/auto-orient/strip%7CimageView2/2/w/520)
在Adapter中直接使用ID
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/27/cf9021981f156d6ba7d47b0e40937e07)
Android Studio也会导入相应的包,格式一般为
import kotlinx.android.synthetic.main..view.*
此处RecyclerVIew的布局文件为recycler_item ,因此这里为
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/27/a21ecb7546b8cf452b27e860007092a6)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/27/21f4b4725c253537a5418ebae1cdd482)
在小弹框中选择Class,输入名称即可,我这里是RecyclerViewAdapter
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/27/e684ed72017ac7d200d0ed25287493c8)
不同于平常的Adapter,Kotlin的Adapter可以在类名后直接加入调用时要传入的参数,相当于Java类中的Adapter的构造函数,Viewholder也不需要findViewById,也是直接使用ID,这里贴出代码
在Activity中使用也相对简单,Kotlin 的for循环相对于java也有不同,不用声明 i变量,代码中的?意思我理解为否,?=也就是不等于空,这也是Kotlin相对于Java的优点,可以避免空指针,缺少?,AS会报错提示,或者可以再null后面添加!!,断言不为空。代码中..表示xx到xx,也就是一个范围
运行如图
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/27/b8b232cd298e4ec1953ea49db3b97562)
代码地址:KotlinDemo
喜欢用简书的朋友,可以浏览简书下的博客:使用Kotlin开发Android 扩展函数(Extensions)
Kotlin Android Extensions
我们在使用Java时,findViewById是比较麻烦的,通过控件的ID来查找控件,当然也有第三方框架ButterKnife,Dagger等来减少findViewById的使用,通过插件自动生成,但在使用Kotlin时,便不必如此。在app的Gradle下添加apply plugin: 'kotlin-android-extensions'
使用扩展函数的好处
在Kotlin官方文档中对ButterKnife类似的库是这样介绍Being libraries dependent on runtime, they require annotating fields for each View
也就是说类似ButterKnife这类库,在运行时,需要对每一个控件进行注解。而对于Kotlin, 官方是这样介绍
The Kotlin Android Extensions plugin allows us to obtain the same experience we have with some of these libraries, without having to add any extra code or shipping any additional runtime.
Kotlin提供类似于这些库的相同体验,但不需要额外的代码和运行时间。我自己理解为减少findViewById的使用的同时,没有产生额外的代码,也不用在运行时对每个控件进行注解。
第一个项目 木子饼干
然后在xml文件里给TextView和Button添加一个Id,直接在MainActivity中import kotlinx.android.synthetic.main.activity_main.* override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) main_text.text = "木子饼干" main_button.setOnClickListener { val intent =Intent(this,RecyclerViewActivity::class.java) startActivity(intent) } }
注意要导包import kotlinx.android.synthetic.main.activity_main.*,一般情况下Android Studio会自动添加这一句,格式一般为
import kotlinx.android.synthetic.main..*
为控件所在的布局文件,不同于Java ,在声明一个变量时,需要在变量前说明类型,Kotlin 一般使用val 和 var, var声明的变量是可变的,val 声明的变量是不可变的,同时也省略new关键字。运行如图
在Adapter中的用法也类似,在布局文件中给控件添加ID,
![](http://upload-images.jianshu.io/upload_images/3030704-8873bf18f558f7ab.png<br/>4000<br/>?imageMogr2/auto-orient/strip%7CimageView2/2/w/520)
在Adapter中直接使用ID
Android Studio也会导入相应的包,格式一般为
import kotlinx.android.synthetic.main..view.*
此处RecyclerVIew的布局文件为recycler_item ,因此这里为
简单的列表展示Demo
和新建Java类类似,新建Kotlin类只要在New的时候选择Kotlin File/Class在小弹框中选择Class,输入名称即可,我这里是RecyclerViewAdapter
不同于平常的Adapter,Kotlin的Adapter可以在类名后直接加入调用时要传入的参数,相当于Java类中的Adapter的构造函数,Viewholder也不需要findViewById,也是直接使用ID,这里贴出代码
class RecyclerViewAdapter(val context: Context, val list: List<String>) : RecyclerView.Adapter<RecyclerViewAdapter.RecyclerHolder>() { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerHolder { return RecyclerHolder(LayoutInflater.from(context).inflate(R.layout.recylcer_item, parent, false)) } override fun getItemCount(): Int { return list.size } override fun onBindViewHolder(holder: RecyclerHolder, position: Int) { val s = list.get(position) holder.itemView.item_text.text = s } class RecyclerHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) { } }
在Activity中使用也相对简单,Kotlin 的for循环相对于java也有不同,不用声明 i变量,代码中的?意思我理解为否,?=也就是不等于空,这也是Kotlin相对于Java的优点,可以避免空指针,缺少?,AS会报错提示,或者可以再null后面添加!!,断言不为空。代码中..表示xx到xx,也就是一个范围
class RecyclerViewActivity : AppCompatActivity() { var adapter:RecyclerViewAdapter ?= null // var adapter:RecyclerViewAdapter = null!! var list =ArrayList<String>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_recycler_view) initView() } fun initView() { for (i in 0 ..20){ list.add("木子饼干" + i) } recyclerView.layoutManager = LinearLayoutManager(this) adapter = RecyclerViewAdapter(this,list) recyclerView.adapter = adapter } }
运行如图
代码地址:KotlinDemo
相关文章推荐
- 让你的代码量减少3倍!使用kotlin开发Android(二) --秘笈!扩展函数
- 让你的代码量减少3倍!使用kotlin开发Android(二) --秘笈!扩展函数
- 使用Kotlin开发Android应用(II):创建新工程
- [Android]使用Kotlin+Anko开发Android(一)
- 使用Kotlin进行Android开发
- 【Android】使用Kotlin在Android Studio上开发App
- 使用Kotlin开发Android应用(III):扩展函数和默认值
- 使用Kotlin&Anko, 扔掉XML开发Android应用
- 使用Kotlin进行Android开发
- android使用kotlin开发基础(一)之 hello kotlin
- 让你的代码量减少3倍!使用kotlin开发Android(三) 缩短五倍的Java Bean
- 使用Kotlin开发Android
- 使用Kotlin开发Android应用
- 使用Kotlin进行Android开发
- 让你的代码量减少3倍!使用kotlin开发Android(一)
- 使用Kotlin开发Android应用(II):创建新工程
- 使用Kotlin开发Android应用(II):创建新工程
- 如果你现在学Android---学习使用Kotlin进行Android开发
- 使用Kotlin进行Android开发
- 让你的代码量减少3倍!使用kotlin开发Android(一)