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编程之Kotlin Android Extensions(扩展插件)_第1张图片

导入合成属性


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

1. 在Activity中

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

Kotlin编程之Kotlin Android Extensions(扩展插件)_第2张图片

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

Kotlin编程之Kotlin Android Extensions(扩展插件)_第3张图片

最终,简洁的代码如下:

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原理

更多相关文章

  1. Android布局之RelativeLayout学习
  2. android 代码生成布局
  3. Android六大基本布局详解
  4. Android对弹出输入法界面影响app界面布局
  5. android:name属性加不加“.”
  6. android一些属性的总结
  7. Android XML属性在文档中的位置

随机推荐

  1. [android]android命令行截图
  2. Android应用程序的权限列表
  3. Android在应用中固定屏幕方向
  4. android ExpandableListView简单例子
  5. OpenGL ES for Android 绘制线
  6. 修改ZXing for Android为竖屏模式
  7. [android警告] AndroidManifest.xml警告
  8. Android中RelativeLayout的字符水平(垂直
  9. 界面布局
  10. 【Android工场】Android Input System介