【ListView】

简单(String)

先为ListView 指定一个id,然后将宽度和高度都设置为match_parent,这样ListView 就占满了整个布局的空间。

xml 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
	android:layout_width="match_parent" 
	android:layout_height="match_parent">
	<ListView
		android:id="@+id/listView" 
		android:layout_width="match_parent"
		android:layout_height="match_parent" />
</LinearLayout>
kt 复制代码
class MainActivity : AppCompatActivity() {
	private val data = listOf("Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango")
	
	override fun onCreate(savedInstanceState: Bundle?) { 
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_main)
		val adapter = 
			ArrayAdapter<String>(
				this, //Activity 的实例
				android.R.layout.simple_list_item_1, //ListView 子项布局的id
				data //数据源
			)
		//用ListView 的setAdapter()方法,将构建好的适配器对象传递进去
		listView.adapter = adapter
	} 
}

定制 item(Fruit)

kt 复制代码
class Fruit(val name:String, val imageId: Int)
xml 复制代码
<!-- fruit_item.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
	android:layout_width="match_parent"
	android:layout_height="60dp">
	<ImageView
	android:id="@+id/fruitImage" 
	android:layout_width="40dp" 
	android:layout_height="40dp" 
	android:layout_gravity="center_vertical" 
	android:layout_marginLeft="10dp"/>
	<TextView
	android:id="@+id/fruitName" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"
	android:layout_gravity="center_vertical" 
	android:layout_marginLeft="10dp" />
</LinearLayout>
kt 复制代码
// FruitAdapter 定义了一个主构造函数:
class FruitAdapter(
	activity: Activity, //Activity 的实例
	val resourceId: Int, //ListView 子项布局的id
	data: List<Fruit> //数据源
) : ArrayAdapter<Fruit>(activity, resourceId, data) {
	//重写了getView()方法,这个方法在每个子项被滚动到屏幕内的时候会被调用。
	override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 
		//首先,使用LayoutInflater来为这个子项加载我们传入的布局
		val view = LayoutInflater.from(context).inflate(resourceId, parent, false)
		//接下来,调用View的findViewById()方法,分别获取到ImageView 和TextView 的实例
		val fruitImage: ImageView = view.findViewById(R.id.fruitImage)
		val fruitName: TextView = view.findViewById(R.id.fruitName)
		
		//然后,通过getItem()方法得到当前项的Fruit实例
		val fruit = getItem(position)

		//分别调用它们的setImageResource()和setText()方法设置显示的图片和文字
		if (fruit != null) {
			fruitImage.setImageResource(fruit.imageId)
			fruitName.text = fruit.name 
		}
		
		return view 
	}
}
kt 复制代码
class MainActivity : AppCompatActivity() {
	private val fruitList = ArrayList<Fruit>()
	override fun onCreate(savedInstanceState: Bundle?) { 
		super.onCreate(savedInstanceState) 
		setContentView(R.layout.activity_main) 
	
		initFruits() // 初始化水果数据
		val adapter = FruitAdapter(this, R.layout.fruit_item, fruitList)
		listView.adapter = adapter 
	}
	private fun initFruits() { 
		repeat(2) {
			fruitList.add(Fruit("Apple", R.drawable.apple_pic)) 
			fruitList.add(Fruit("Banana", R.drawable.banana_pic)) 
			fruitList.add(Fruit("Orange", R.drawable.orange_pic)) 
			fruitList.add(Fruit("Watermelon", R.drawable.watermelon_pic)) 
			fruitList.add(Fruit("Pear", R.drawable.pear_pic)) 
			fruitList.add(Fruit("Grape", R.drawable.grape_pic)) 
			fruitList.add(Fruit("Pineapple", R.drawable.pineapple_pic)) 
			fruitList.add(Fruit("Strawberry", R.drawable.strawberry_pic)) 
			fruitList.add(Fruit("Cherry", R.drawable.cherry_pic)) 
			fruitList.add(Fruit("Mango", R.drawable.mango_pic))
		} 
	} 
}

repeat函数是Kotlin 中另外一个非常常用的标准函数,它允许你传入一个数值n,然后会把Lambda 表达式中的内容执行n

提高效率

convertView参数用于将之前加 载好的布局进行缓存,以便之后进行重用:

kt 复制代码
class FruitAdapter(activity: Activity, val resourceId: Int, data: List<Fruit>) : ArrayAdapter<Fruit>(activity, resourceId, data) {
	override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 
		val view: View
		if (convertView == null) {
			view = LayoutInflater.from(context).inflate(resourceId, parent, false)
		} else {
			view = convertView
		}
		...
	}
}

继续优化:

kt 复制代码
class FruitAdapter(activity: Activity, val resourceId: Int, data: List<Fruit>) : ArrayAdapter<Fruit>(activity, resourceId, data) {
	//新增了一个内部类ViewHolder,用于对ImageView和TextView的控件实例进行缓存
	inner class ViewHolder(val fruitImage: ImageView, val fruitName: TextView)
	
	override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 
		val view: View
		val viewHolder: ViewHolder 
		if (convertView == null) {
			view = LayoutInflater.from(context).inflate(resourceId, parent, false) 
			val fruitImage: ImageView = view.findViewById(R.id.fruitImage)
			val fruitName: TextView = view.findViewById(R.id.fruitName) 
			viewHolder = ViewHolder(fruitImage, fruitName)
			//View的setTag()方法,将ViewHolder对象存储在View中
			view.tag = viewHolder
		} else {
			view = convertView
			viewHolder = view.tag as ViewHolder 
		}
		
		val fruit = getItem(position)
		if (fruit != null) {
			viewHolder.fruitImage.setImageResource(fruit.imageId)
			viewHolder.fruitName.text = fruit.name 
		}
		return view 
	}
}
相关推荐
姑苏风3 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
数据猎手小k6 小时前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
你的小107 小时前
JavaWeb项目-----博客系统
android
风和先行7 小时前
adb 命令查看设备存储占用情况
android·adb
AaVictory.8 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
似霰9 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
大风起兮云飞扬丶9 小时前
Android——网络请求
android
干一行,爱一行9 小时前
android camera data -> surface 显示
android
断墨先生9 小时前
uniapp—android原生插件开发(3Android真机调试)
android·uni-app
无极程序员11 小时前
PHP常量
android·ide·android studio