安卓3.0 的kotlin是用listview

为了实现listview 我们需要实现一个adapter 

       list view中样式的布局现就一个TextView,我会手动去创建这个简单的文本列表。增加一个名为TestAdapter文件,包括如下代码:

TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

在你重写方法时这段代码要删除,不然直接会报错,现在直接上  adapter  的代码 

@Suppress("UNREACHABLE_CODE")
class TestAdapter(var itemList: ArrayList<String>, var context: Context) : BaseAdapter() {
    override fun getCount(): Int {
        return itemList.size
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var hoder: TestViewhoder
        val view: View
        if (convertView == null) {
            view = View.inflate(context, R.layout.item, null)
            hoder = TestViewhoder(view)
            view.tag = hoder
        } else {
            view = convertView
            hoder = view.tag as TestViewhoder
        }
        hoder.str.text = itemList[position]

        return view
    }

    override fun getItem(position: Int): Any {
        return itemList.get(position)
    }

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

}

class TestViewhoder(var viewItem: View) {
    var str: TextView = viewItem.findViewById(R.id.str) as TextView
}

然后是样式布局

<?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">

    <TextView
        android:id="@+id/str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

在mainActivity里面实现  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">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_marginTop="60dp"
        android:src="@mipmap/ic_launcher" />
    <Button
        android:id="@+id/jion"
        android:layout_marginTop="20dp"
        android:background="@color/colorPrimary"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:textSize="20sp"
        />
    <ListView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lvlist"
        android:saveEnabled="false"
        />
</LinearLayout>

实现方法

@Suppress("UNREACHABLE_CODE")
class MainActivity : AppCompatActivity() {
    var itemList = ArrayList<String>()
    private var adapter: TestAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        inidata()
        adapter = TestAdapter(itemList, this)
        jion.setOnClickListener({
            //            Toast.makeText(this,"进入" ,Toast.LENGTH_SHORT).show()
            lvlist.adapter = adapter
        })
    }

这里给集合添加数据的方式多种多样,这添加方式通熟易懂
    fun inidata() {
        var i: Int = 0
        var end: Int = 10
        while (i < end) {
            itemList.add("" + i)
            i++
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃鸳鸯锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值