說好今天要寫一個使用Kotlin寫Adapter的列子,我想了半天也沒有組織好語言,直接上代碼吧,有一定Android基礎(chǔ)的小伙伴肯定是能看的懂的
package com.example.administrator.kotlintestimport android.content.Contextimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport android.widget.BaseAdapterimport android.widget.TextView/** * Created by zhanglei on 2017/5/31. * ListView的適配器 */class MyAdapter(var mList: List<String>, var context: Context) : BaseAdapter() { override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { var holder: MyViewHolder //重用view var v: View if (convertView == null) { holder = MyViewHolder() v = LayoutInflater.from(context).inflate(R.layout.list_item_layout, parent, false) holder.textView = v.findViewById(R.id.list_text_item) as TextView //設(shè)置tag v.tag = holder } else { v = convertView //獲取tag并強轉(zhuǎn) holder = v.tag as MyViewHolder } //為TextView設(shè)置內(nèi)容 holder.textView.text = mList[position] return v } override fun getItem(position: Int): Any { return mList[position] } override fun getItemId(position: Int): Long { return position.toLong() } override fun getCount(): Int { return mList.size } class MyViewHolder { lateinit var textView: TextView }}
先寫成這樣吧,有什么不懂的請在評論區(qū)留言,我能幫你解決的我會盡力的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答
圖片精選