Kotlin Android Extensions使用指南

2,827 阅读1分钟

一、项目中如何配置

Kotlin Android Extensions是Kotlin团队开发的一个插件,目的是让我们在开发过程中更少的编写代码。目前包括了视图绑定的功能。

在Module中的build.gradle文件添加插件配置

apply plugin: 'kotlin-android-extensions'

或另一种写法

plugins {
    ...
    id 'kotlin-android-extensions'  //这一行
}

在xml文件中给view加上id就可以直接引用了

二、项目中如何使用

所有用法的布局跟Activity的布局一样,只是名字不同而已

1、在Activity中

xml文件中没有什么特别,日常写法,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv"   //添加了id
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Actiity中使用

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*  //导入这个


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //可以直接引用了
        iv.setOnClickListener{
            
        }
    }
}

2、Fragment中使用

import kotlinx.android.synthetic.main.layout_test_fragment.*  //导入这个

class TestFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //给定Fragment的布局
        return inflater.inflate(R.layout.layout_test_fragment,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //可以直接引用了
        iv.setOnClickListener {

        }
    }
}

3、Adapter中使用

import kotlinx.android.synthetic.main.layout_test_adapter.view.*  //导入这个

class TestAdapter(var context: Context) : RecyclerView.Adapter<TestAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var view = LayoutInflater.from(context).inflate(R.layout.layout_test_adapter, parent, false)
        return ViewHolder(view)  //绑定View
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //可以引用了
        holder.itemView.iv.setOnClickListener {  }
    }

    override fun getItemCount(): Int {
        TODO("Not yet implemented")
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}

4、自定义View中使用

import kotlinx.android.synthetic.main.layout_test_view.view.*  //导入这个

class TestView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet) :
    View(context, attributeSet) {

    init {
        View.inflate(context, R.layout.layout_test_view, null)
        //可以引用了
        iv.setOnClickListener { }
    }
}

个人学习笔记

参考以下博客,表示感谢!

# Kotlin Android Extensions使用指南