一:project-build.gradle代码
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.hexl.mykotlin1"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
二:app-builde.gralde
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.30'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
三:实现代码
package com.hexl.mykotlin1
import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
//初始化数据源
val data = listOf("hexl", "Dongl", "Dongl", "Dongl", "Dongl", "Dongl", "Dongl", "Dongl",
"Dongl", "Dongl", "Dongl", "Dongl", "Dongl", "Dongl", "Dongl", "Dongl", "Dongl",
"Dongl", "Dongl", "Dongl", "Dongl")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//设置布局
setContentView(R.layout.activity_main)
//绑定id,这里的泛型需要指定view,kotlin会自动推断类型。
val recyclerView = findViewById<RecyclerView>(R.id.rv)
//设置布局管理器
recyclerView.layoutManager = LinearLayoutManager(this)
//初始化适配器
val adapter = MyAdapter(this, data)
//设置适配器
recyclerView.adapter = adapter
//item点击事件
adapter.setOnItemClickListener(listener = object : MyAdapter.OnItemClickListener {
override fun setOnItemClickListener(view: View, int: Int) {
//kotlin 不能使用 MainActivity.this 这种方式获取 Context 对象
// this@MainActivity、MainActivity@this,通过这两种方式获取
Toast.makeText(this@MainActivity, int.toString(), Toast.LENGTH_LONG).show()
}
})
}
}
/**
* 适配器
* 通过构造函数将 上下文 和 数据源 传到适配器里
* 也可以采用次构造函数constructor()
*/
class MyAdapter(private val context: Context, private val data: List<String>) : RecyclerView.Adapter<MyAdapter.MyVH>() {
private var listener: OnItemClickListener? = null
// private var context1: Context? = null
// private var data1: ArrayList<String>? = null
// constructor(context1: Context, data1: ArrayList<String>) {
// this.context1 = context1
// this.data1 = data1
// }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
return MyVH(LayoutInflater.from(context).inflate(R.layout.item_rv, parent, false))
}
override fun getItemCount(): Int {
return data.size
}
override fun onBindViewHolder(viewholder: MyVH, position: Int) {
viewholder.textView.text = data[position]
viewholder.itemView.setOnClickListener {
//!! 等价于 ? 检测是否为null
listener!!.setOnItemClickListener(view = viewholder.itemView, int = position)
}
}
fun setOnItemClickListener(listener: OnItemClickListener) {
this.listener = listener
}
class MyVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textView: TextView = itemView.findViewById(R.id.tv_text)
}
interface OnItemClickListener {
fun setOnItemClickListener(view: View, int: Int)
}
}