kotlin实现简单的GridView
使用kotlin实现简单的GridView
xml代码
activity_main代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridView
android:layout_width="1600px"
android:layout_height="800px"
android:id="@+id/gridView"
android:layout_centerInParent="true"
android:numColumns="2"/>
</RelativeLayout>
GridView子项布局grid_view代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Realtive">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="25sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
kotlin代码
实体类代码
package com.example.yyr2_7
/**
*@创建者: yuyiran
*@创建日期: 2020-5-26 9:07
*@描述: 数据类
*/
class IotResource(val imageId: Int,val text:String,val colorId:Int) {
}
适配器代码
package com.example.yyr2_7
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
/**
*@创建者: yuyiran
*@创建日期: 2020-5-26 8:57
*@描述: 适配器
*/
class MyAdapter (var context: Context,var datas:ArrayList<IotResource>) : BaseAdapter(){
inner class MyHolder(){
lateinit var imageId : ImageView
lateinit var text : TextView
lateinit var mRelative : RelativeLayout
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
// var view:View ?= null
// var myHolder:MyHolder ?= null
var view:View ?= null
var myHolder:MyHolder ?= null
if(convertView == null){
myHolder = MyHolder()
view = LayoutInflater.from(context).inflate(R.layout.grid_view,null)
myHolder.imageId = view.findViewById(R.id.image)
myHolder.text = view.findViewById(R.id.textView)
myHolder.mRelative = view.findViewById(R.id.Realtive)
view.tag = myHolder
}else{
view = convertView
myHolder = view.tag as MyHolder
}
myHolder.text.setText(datas.get(position).text)
myHolder.imageId.setImageResource(datas.get(position).imageId)
myHolder.mRelative.setBackgroundColor(datas.get(position).colorId)
return view!!
}
override fun getItem(position: Int): Any {
//获取指定位置(position)上的item对象,通常不需要修改
return datas.get(position)
}
override fun getItemId(position: Int): Long {
// 获取指定位置(position)上的item的id,通常不需要修改
return position.toLong()
}
override fun getCount(): Int {
//返回一个整数,就是要在listview中现实的数据的条数
return datas.size
}
}
MainActivity代码
package com.example.yyr2_7
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
var list:ArrayList<IotResource> ?= ArrayList()
var texts = arrayOf<String>("环境气象","智能商场","预警信息","智能农场")
var pics = arrayOf<Int>(R.drawable.icon_weather,R.drawable.icon_shopping,R.drawable.icon_security,R.drawable.icon_agriculture)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initIotResource()
gridView.adapter = MyAdapter(this,list!!)
}
private fun initIotResource(){
for(i in 1..100){
val randomNum = (0..3).random()
// val text = texts[(0..3).random()]
// val pic = pics[i]
// val colors = RandomColor()
list?.add(IotResource(pics[randomNum],texts[randomNum],RandomColor()))
// list?.add(IotResource(pics[i],"texts[i]",RandomColor()))
}
}
private fun RandomColor():Int{
var r = Random.nextInt(256)
var g = Random.nextInt(256)
var b = Random.nextInt(256)
return Color.rgb(r,g,b)
}
}