Kotlin 与 listView

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YYtomorrow/article/details/75734699

Kotlin与java语法不同,就android的开发还是有好多坑值得注意的

用kotlin实现listview。

首先是MainActivity

package com.example.administrator.kotlindemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    lateinit var adapter: TestAdapter
    var items = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        adapter = TestAdapter(this)
        test_list.adapter = adapter

        for (i in 1..15) {
            items.add("test_" + i)
        }

        adapter.setData(items)
    }
}

这里的lateinit关键字表示延时初始化

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/test_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />

</LinearLayout>


TestAdapter

package com.example.administrator.kotlindemo

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView

/**
 * Created by Administrator on 2017/7/22.
 */
class TestAdapter(context: Context) : BaseAdapter() {

    var context = context

    var items = ArrayList<String>()

    fun setData(items: ArrayList<String>) {
        this.items = items
        notifyDataSetChanged()
    }

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        var viewHolder: ViewHolder
        var view: View

        if (p1 == null) {
            view = View.inflate(context, R.layout.listitem_test, null)
            viewHolder = ViewHolder(view)
            view.setTag(viewHolder)
        } else {
            view = p1
            viewHolder = view.getTag() as ViewHolder
        }

        viewHolder.item_test.text = items.get(p0)

        return view
    }

    override fun getItem(p0: Int): Any {
        return items.get(p0)
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return items.size
    }

    class ViewHolder(var v: View) {
        var item_test: TextView = v.findViewById(R.id.item_test)
    }

}


listitem_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/item_test"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center" />

</LinearLayout>

阅读更多
换一批

没有更多推荐了,返回首页