ListView的用法介绍。
2个组件, TextView,ListView依次纵向线性布局。
ViewItem的布局,左边图片,右边文字水平排列:
代码语言:javascript复制
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<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="80dp"
android:layout_height="80dp"
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>
Activity的布局
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id = "@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text = "Hello, fruits"
android:textSize = "20sp"/>
<ListView
android:id = "@ id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
</LinearLayout>
Kotlin 代码:
代码语言:javascript复制
代码语言:javascript复制package com.example.listviewtest
import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class Fruit(val name:String, val imageID:Int){
}
class FruitAdapter(activity: Activity, val resourceId:Int, data :List<Fruit>): ArrayAdapter<Fruit>(activity, resourceId, data){
inner class ViewHolder(val fruitImage:ImageView, val fruitName:TextView) //inner calss 表示内部类
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.tag = viewHolder
}
else {
view = convertView
viewHolder = view.tag as ViewHolder
}
val fruit = getItem(position)// 获取当前项的Fruit实例
if(fruit != null){
viewHolder.fruitImage.setImageResource(fruit.imageID)
viewHolder.fruitName.text = fruit.name
}
return view
}
}
class MainActivity : AppCompatActivity() {
//private val data = listOf("Apple", "Banana","Orange","Watermelon", "Pear","Grape", "Pineapple",
//"Strawberry","Cherry","Mango")
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
listView.setOnItemClickListener { parent, view, position, id ->
val fruit = fruitList[position]
textView.setText("选中了第" (position 1).toString() "个水果-->" fruit.name)
//Toast.makeText(this, fruit.name "," position.toString(), Toast.LENGTH_SHORT).show()
}
}
private fun initFruits(){
repeat(2){
fruitList.add(Fruit("Apple(苹果)", R.drawable.apple_pic))
fruitList.add(Fruit("Banana(香蕉)", R.drawable.banana_pic))
fruitList.add(Fruit("Cherry(樱桃)", R.drawable.cherry_pic))
fruitList.add(Fruit("Grape(葡萄)", R.drawable.grape_pic))
fruitList.add(Fruit("Mango(芒果)", R.drawable.mango_pic))
fruitList.add(Fruit("Orange(橘子)", R.drawable.orange_pic))
fruitList.add(Fruit("Pear(梨子)", R.drawable.pear_pic))
fruitList.add(Fruit("Pineapple(菠萝)", R.drawable.pineapple_pic))
fruitList.add(Fruit("Strawberry(草莓)", R.drawable.strawberry_pic))
fruitList.add(Fruit("Watermelon(西瓜)", R.drawable.watermelon_pic))
}
}
}
代码语言:javascript复制
AndroidManifest
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.listviewtest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ListViewTest">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>