Kotlin编程之Kotlin Android(安卓)Extensions(扩展插件)
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) : RecyclerView.Adapter() { 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中对应的布局:
"http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:fitsSystemWindows="true" android:layout_height="match_parent"> //......... .support.v7.widget.RecyclerView android:id="@+id/movieList_recyclverView" android:layout_width="match_parent" android:layout_height="match_parent"> .support.v7.widget.RecyclerView>
在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原理
更多相关文章
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android(安卓)UI开发第二十八篇――Fragment中使用左右滑动菜单
- android shape的使用
- Android——编译release版签名系统
- Android上使用libgdx