从Android项目学习Kotlin(二)

一、基本类型

从Android项目学习Kotlin(二)_第1张图片
image.png

定义变量:

 var a:Int=1

也可以

val a:Int=1

也可以简写,自动转换

var a=1

var和val的区别是,var定义的变量可以变,val定义的变量不可变,val,相当于java的final,注意kotlin不需要以分号结尾。
类型间转换

 toByte(): Byte
 toShort(): Short
 toInt(): Int
 toLong(): Long
 toFloat(): Float
 toDouble(): Double
 toChar(): Char

二、控制语句

if语句

val a:Int=1
val b:Int=3
val max = if (a > b) a else b

When 表达式

  when (dataItem.type) {
        "Android" -> holder?.ivType?.setImageResource(R.mipmap.android_icon)
        "iOS" -> holder?.ivType?.setImageResource(R.mipmap.ios_icon)
        "前端" -> holder?.ivType?.setImageResource(R.mipmap.js_icon)
        "拓展资源" -> holder?.ivType?.setImageResource(R.mipmap.other_icon)
        else -> holder?.ivType?.setImageResource(R.mipmap.android_icon)
    }

For 循环
for 循环可以对任何提供迭代器(iterator)的对象进行遍历,语法如下:

     for (item in collection) print(item)

三、接口、属性、类

类定义

 class Person(age: Int, name: String) {
   }

使用不需要new

  val person = Person(11, "张三")
 print(person.toString)

默认下任何类都是继承自Any( 类似 java中的Object,但是Any 不是 java.lang.Object ;尤其是,它除了 equals() 、 hashCode() 和 toString() 外没有任何成员) ,但是我们可以继承其它类。
所有的类默认都是不可继承的(final),所以我们只能继承那些明确声明open或者abstract的类:

  open class Animal(name: String)
  class Person(name: String, surname: String) : Animal(name)

Android适配器的ViewHolder类

 inner class GirlHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    internal var ivGril: ImageView = itemView.findViewById(R.id.iv_girl)
 }

Android适配器的ViewHolder类(Kotlin代码 不用缩写 使用init初始化方法)

inner class HomeHolder(item: View) : RecyclerView.ViewHolder(item) {
    internal var ivType: ImageView
    internal var tvType: TextView
    internal var ivAuthor: ImageView
    internal var tvAuthor: TextView
    internal var tvTime: TextView
    internal var rlMessage: RelativeLayout
    internal var ivPart: ImageView
    internal var ivVedio: ImageView
    internal var tvItem: TextView

    init {
        ivType = itemView.findViewById(R.id.iv_type)
        tvType = itemView.findViewById(R.id.tv_type)
        ivAuthor = itemView.findViewById(R.id.iv_author)
        tvAuthor = itemView.findViewById(R.id.tv_author)
        tvTime = itemView.findViewById(R.id.tv_time)
        rlMessage = itemView.findViewById(R.id.rl_message)
        ivPart = itemView.findViewById(R.id.iv_part)
        ivVedio = itemView.findViewById(R.id.iv_vedio)
        tvItem = itemView.findViewById(R.id.tv_item)
    }
}

函数(方法)可以使用fun关键字进行定义:

 fun add(x: Int, y: Int) : Int {
   return x + y
 }

也可以

     fun add(x: Int,y: Int) : Int = x + y

接口:

     interface ItemClickListener {
          fun onItemClickListener(url:String)
    }

点击回调

     girlAdapter?.setOnItemClickListener(object : GirlAdapter.ItemClickListener {
        override fun onItemClickListener(url: String) {
            val intent = Intent()
            val bun = Bundle()
            bun.putString("url", url)
            //获取intent对象
            intent.setClass(activity, SeeBeautyActivity::class.java)
            // 获取class是使用::反射
            intent.putExtra("extra", bun)
            startActivity(intent)
        }
    })

完整的适配器

class GirlAdapter(context: Context) : RecyclerView.Adapter() {
private var data = ArrayList()
private var context: Context = context
private var onItemClickListener: ItemClickListener? = null
fun setOnItemClickListener(onItemClickListener: ItemClickListener) {
    this.onItemClickListener = onItemClickListener
}

fun setData(data: List) {
    this.data.clear()
    this.data.addAll(data)
    notifyDataSetChanged()
}

fun addData(data: List) {
    this.data.addAll(data)
    notifyDataSetChanged()
}

override fun onBindViewHolder(holder: GirlHolder?, position: Int) {
    Glide.with(context).load(data[position].url).into(holder?.ivGirl)
    holder?.itemView?.setOnClickListener {
        onItemClickListener?.onItemClickListener(data[position].url)
    }
}

override fun getItemCount(): Int = data.size

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): GirlHolder {
    return GirlHolder(LayoutInflater.from(context).inflate(R.layout.adapter_gril, parent, false))
}

inner class GirlHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    internal var ivGirl: ImageView = itemView.findViewById(R.id.iv_girl)
}

interface ItemClickListener {
    fun onItemClickListener(url:String)
}
}

你可能感兴趣的:(从Android项目学习Kotlin(二))