ListView自帶兩個函數(shù):
- addFooterView
- addHeaderView
可以直接調(diào)用雳锋,并且可以多次調(diào)用哺壶;
//獲取控件
listView = findViewById(R.id.listView)
listView.adapter =
MyAdapter(
this,
fruitList
)
listView.setOnItemClickListener { parent, view, position, id ->
Toast.makeText(this,fruitList[position].name,Toast.LENGTH_SHORT).show()
}
val headerLayout: LinearLayout = LayoutInflater.from(this@ListViewActivity5)
.inflate(R.layout.layout_listview_header1, null) as LinearLayout
initHeadListView(headerLayout)
listView.addHeaderView(headerLayout)
val footerLayout: LinearLayout = LayoutInflater.from(this@ListViewActivity5)
.inflate(R.layout.layout_listview_footer1, null) as LinearLayout
val footerLayout2: LinearLayout = LayoutInflater.from(this@ListViewActivity5)
.inflate(R.layout.layout_listview_footer2, null) as LinearLayout
//header 和 footer 可以重復(fù)添加,可以添加多個
listView.addFooterView(footerLayout)
listView.addFooterView(footerLayout2)
// listView.removeFooterView(footerLayout)
/**
* 初始化頭部控件布局初始化
*/
private fun initHeadListView(view: View) {
view.findViewById<LinearLayout>(R.id.ll_header_left).apply {
this.layoutParams.width = 200
}
}