Kotlin编程之Kotlin Android Extensions(扩展插件)
来源:互联网 发布:淘宝网男士休闲布鞋 编辑:程序博客网 时间:2024/06/06 05:30
Kotlin编程开发Android运用程序的相关介绍:
- Kotlin编程之AndroidStudio(包括3.0与2.x版本)配置与使用
- Kotlin编程开发Android运用程序(Volley+Gson依赖库)
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原理
- Kotlin编程之Kotlin Android Extensions(扩展插件)
- kotlin初窥之Kotlin Android Extensions
- Kotlin学习之kotlin-android-extensions
- 使用Kotlin开发Android 扩展函数(Extensions)
- < Kotlin > Kotlin Android Extensions (译文)
- Use Kotlin Android Extensions
- Kotlin编程之扩展方法
- android之kotlin编程
- Kotlin开发Android笔记10:Kotlin中Kotlin Android Extensions
- Kotlin学习(九): 扩展(Extensions)和This表达式
- Kotlin Android Extensions工具使用
- Kotlin Android 扩展(二)
- Kotlin-12.扩展函数和属性(extensions)
- kotlin in android 学习android extensions(一)
- Kotlin类之扩展
- Kotlin Android 扩展
- android kotlin扩展函数
- Android扩展Kotlin
- Unity使用过程中遇到的问题
- Linux常见命令
- lucene--DocInverterPerField/DocInverterPerField
- 密码8到64位大小写字母+数字+字符 不能包含空格
- 高逼格关键词文摘--Peter
- Kotlin编程之Kotlin Android Extensions(扩展插件)
- erlang 17.1 centos6.5 安装
- Tomcat启动的几种方式
- My97DatePicker与angular一起使用时改变了但是angular无法监测到变化解决方式
- UDP 组播---基本概念
- javascript中阻塞和非阻塞,同步和非同步的区别
- 35. Search Insert Position
- 微信小程序实战篇-电商(二)
- 根据字段名称找到数据库和表的sql