关注 安卓007 ,免费获取全套安卓开发学习资料
ListView是用于显示列表的UI控件.不过当前主流的已经是RecyclerView了,更强大,更好用.
ListView
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
private fun initListView() {
var dataList = getData()
val adapter = ListViewAdapter(this)
adapter.setData(dataList)
listView.adapter = adapter
//隐藏自带的分隔符,在ListViewAdapter对应的布局里自定义分隔符,这样可以更灵活。
listView.divider = null
//添加点击事件
listView.setOnItemClickListener { adapterView, _, position, _ ->
val text = adapterView.getItemAtPosition(position).toString()
toast(text)
}
}
private fun getData(): List<String> {
val dataList = ArrayList<String>()
for (index in 0 until 100) {
val text = " 数据$index "
dataList.add(text)
}
return dataList
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initListView()
}
private fun initListView() {
var dataList = getData()
val adapter = ListViewAdapter(this)
adapter.setData(dataList)
listView.adapter = adapter
//隐藏自带的分隔符,在ListViewAdapter对应的布局里自定义分隔符,这样可以更灵活。
listView.divider = null
//添加点击事件
listView.setOnItemClickListener { adapterView, _, position, _ ->
val text = adapterView.getItemAtPosition(position).toString()
toast(text)
}
}
private fun getData(): List<String> {
val dataList = ArrayList<String>()
for (index in 0 until 100) {
val text = " 数据$index "
dataList.add(text)
}
return dataList
}
/**
* 显示toast短暂提示
*/
private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
<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>
class ListViewAdapter(var context: Context) : BaseAdapter() {
private var mDataList = mutableListOf<String>()
/**
* 设置数据
*/
fun setData(dataList: List<String>) {
mDataList.clear()
mDataList.addAll(dataList)
//数据变更后,需要调用notifyDataSetChanged通知数据变化。
notifyDataSetChanged()
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
var viewHolder: ViewHolder
var view: View
//下面对convertView进行判断,是为了复用已存在的视图,提升效率。
if (convertView == null) {
view = View.inflate(context, R.layout.item_layout, null);
viewHolder = ViewHolder(view)
view.tag = viewHolder
} else {
view = convertView
viewHolder = view.tag as ViewHolder
}
//获取当前位置数据
val item = getItem(position)
//显示用户名
if (item is String) {
viewHolder.nameTv.text = item
}
return view
}
override fun getItem(position: Int): Any {
return mDataList[position]
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getCount(): Int {
return mDataList.size
}
class ViewHolder(view: View) {
var nameTv: TextView = view.findViewById(R.id.nameTv)
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
tools:text="姓名" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="5dp"
android:background="#E7E7E7" />
LinearLayout>
https://gitee.com/cxyzy1/ListViewDemo
Kotlin语言基础
UI控件_TextView
UI控件_EditText
UI控件_Button
UI控件_ImageView
UI控件_RadioButton
UI控件_CheckBox
UI控件_ProgressBar
关注头条号,第一时间获取最新文章: