关于android:Android进阶Kotlin实现listview

36次阅读

共计 2552 个字符,预计需要花费 7 分钟才能阅读完成。

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>

正文完
 0