什么是ListView
ListView是用于显示列表的UI控件.不过当前主流的已经是RecyclerView了,更强大,更好用.
基础样例
效果图
方案简要介绍
在activity对应的布局文件中增加ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
新增一个Adapter用于展示列表中每一行内容(含对应布局文件)
详见下面完整代码中的ListViewAdapter及其布局文件.
在activity中实例化Adapter,设置数据,并将adapter设置给ListView
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 {
val dataList = ArrayList()
for (index in 0 until 100) {
val text = " 数据$index "
dataList.add(text)
}
return dataList
}
完整代码
activity代码:MainActivity
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 {
val dataList = ArrayList()
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()
}
MainActivity对应布局文件: activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
ListView对应Adapter: ListViewAdapter
class ListViewAdapter(var context: Context) : BaseAdapter() {
private var mDataList = mutableListOf()
/**
* 设置数据
*/
fun setData(dataList: List) {
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)
}
}
ListViewAdapter对应布局文件: item_layout.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
tools:text="姓名" />
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="5dp"
android:background="#E7E7E7" />
基础样例完整源代码