美文网首页
【Android进阶】Kotlin实现listview

【Android进阶】Kotlin实现listview

作者: 易含 | 来源:发表于2022-01-25 09:23 被阅读0次

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



    2.适配器:

    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.布局:

    //主界面
    <?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>
    

    相关文章

      网友评论

          本文标题:【Android进阶】Kotlin实现listview

          本文链接:https://www.haomeiwen.com/subject/wgfwhrtx.html