前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android进阶】Kotlin实现listview

【Android进阶】Kotlin实现listview

原创
作者头像
易寒
发布2022-01-25 09:29:58
5320
发布2022-01-25 09:29:58
举报
文章被收录于专栏:Android知识

1.实现效果图: 其实总体和java也差不多的  就是部分写法稍微有点变化,更加简洁了  写代码更方便了

2.适配器:

代码语言:txt
复制
package com.example.admin.zkotlin

import android.content.Context
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.list_item_color.view.*

class MainActivity : AppCompatActivity() {
    val colors = arrayOf("red","green","blue","cyan",
            "mageenta","yellow","black","white"
    ,"gray","maroon","fuchsia","navy","olive","teal")


    val numList = ArrayList<Int>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 遍历0-1000的数值
        for (i in 0..20){
            numList.add(i)
        }
        lvColors.adapter = ColorAdapter(this,numList,colors)


    }
    class ColorAdapter(val context: Context, val numList: ArrayList<Int>, val colors: Array<String>) : BaseAdapter() {
       override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater;
            val itemView = convertView ?: li.inflate(R.layout.list_item_color,parent,false)
            val id = numList[position]
            val colorName = colors[position%colors.size]
            itemView.tvColor.text = colorName
            itemView.tvId.text = id.toString()
            return itemView
        }



        override fun getItem(position: Int): Any? {
            return null
        }

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

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

    }

}

3.布局:

代码语言:txt
复制
//主界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llColorBox"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ListView
        android:id="@+id/lvColors"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

//item界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llColorBox"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tvId"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:text="10"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tvColorText"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="BLACK"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tvColor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp" />
</LinearLayout>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档