一寨蹋、基本類型
定義變量:
var a:Int=1
也可以
val a:Int=1
也可以簡寫擂涛,自動轉(zhuǎn)換
var a=1
var和val的區(qū)別是,var定義的變量可以變聊记,val定義的變量不可變撒妈,val,相當(dāng)于java的final,注意kotlin不需要以分號結(jié)尾排监。
類型間轉(zhuǎn)換
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 表達(dá)式
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 循環(huán)
for 循環(huán)可以對任何提供迭代器(iterator)的對象進(jìn)行遍歷,語法如下:
for (item in collection) print(item)
三社露、接口挟阻、屬性、類
類定義
class Person(age: Int, name: String) {
}
使用不需要new
val person = Person(11, "張三")
print(person.toString)
默認(rèn)下任何類都是繼承自Any( 類似 java中的Object峭弟,但是Any 不是 java.lang.Object ;尤其是脱拼,它除了 equals() 瞒瘸、 hashCode() 和 toString() 外沒有任何成員) ,但是我們可以繼承其它類熄浓。
所有的類默認(rèn)都是不可繼承的(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<ImageView>(R.id.iv_type)
tvType = itemView.findViewById<TextView>(R.id.tv_type)
ivAuthor = itemView.findViewById<ImageView>(R.id.iv_author)
tvAuthor = itemView.findViewById<TextView>(R.id.tv_author)
tvTime = itemView.findViewById<TextView>(R.id.tv_time)
rlMessage = itemView.findViewById<RelativeLayout>(R.id.rl_message)
ivPart = itemView.findViewById<ImageView>(R.id.iv_part)
ivVedio = itemView.findViewById<ImageView>(R.id.iv_vedio)
tvItem = itemView.findViewById<TextView>(R.id.tv_item)
}
}
函數(shù)(方法)可以使用fun關(guān)鍵字進(jìn)行定義:
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)
}
點(diǎn)擊回調(diào)
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<GirlHolder>() {
private var data = ArrayList<GankResults.Item>()
private var context: Context = context
private var onItemClickListener: ItemClickListener? = null
fun setOnItemClickListener(onItemClickListener: ItemClickListener) {
this.onItemClickListener = onItemClickListener
}
fun setData(data: List<GankResults.Item>) {
this.data.clear()
this.data.addAll(data)
notifyDataSetChanged()
}
fun addData(data: List<GankResults.Item>) {
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)
}
}