我想在我的應用程序來實現的ExpandableListView,但它沒有顯示出來。ExpandableListView沒有出現在我的活動
我仔細檢查了:
正確的命名和指對所有我的三個XML佈局(用於ExpandableListView,表頭和列表項)的,
調試的,看我的列表正在填充
prepareListdata()
方法(它們是)。
我覺得問題在我ExpandableListAdapterClass
,作爲 類參數listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>
越來越着色爲 波浪灰線不使用。
如何正確地告訴我的應用程序,這些都是等於我的活動成員變量?還是有什麼明顯的我失蹤?
這裏是我的代碼:
適配器
class ExpandableListAdapter (context: Context, listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>) : BaseExpandableListAdapter() {
var listDataHeader = arrayListOf<String>() //list sub-headers
var listDataChild = HashMap<String, List<String>>() //list sub-points
private val inflater: LayoutInflater = LayoutInflater.from(context)
override fun getGroup(groupPosition: Int): Any {
return this.listDataHeader[groupPosition]
}
override fun isChildSelectable(p0: Int, p1: Int): Boolean {
return true
}
override fun hasStableIds(): Boolean {
return false
}
override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View? {
val headerTitle: Int = getGroup(groupPosition) as Int
if (convertView == null) {
inflater.inflate(R.layout.main_list_group_header, parent, false)
}
val listHeader = convertView?.findViewById<TextView>(R.id.list_header)
listHeader?.setTypeface(null, Typeface.BOLD)
listHeader?.setText(headerTitle)
return convertView
}
override fun getChildrenCount(groupPosition: Int): Int {
return this.listDataChild[this.listDataHeader[groupPosition]]!!.size
}
override fun getChild(groupPosition: Int, childPosition: Int): Any {
return this.listDataChild[this.listDataHeader[groupPosition]]!![childPosition]
}
override fun getGroupId(groupPosition: Int): Long {
return groupPosition.toLong()
}
override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View? {
val childText: Int = getChild(groupPosition, childPosition) as Int
if (convertView == null) {
inflater.inflate(R.layout.main_list_item, parent, false)
}
val textListChild = convertView?.findViewById<TextView>(R.id.list_item)
textListChild?.setText(childText)
return convertView
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long {
return childPosition.toLong()
}
override fun getGroupCount(): Int {
return this.listDataHeader.size
}
}
MainActivity和prepareListData的其餘部分()函數
class MainListActivity : AppCompatActivity() {
lateinit var adapter : ExpandableListAdapter
val listDataHeader = arrayListOf<String>()
val listDataChild = HashMap<String, ArrayList<String>>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mainlistactivity)
prepareListData()
adapter = ExpandableListAdapter(this, listDataHeader, listDataChild)
expandable_list.setAdapter(adapter)
}
private fun prepareListData() {
listDataHeader.add("Gathering data")
listDataHeader.add("Simulate")
val gatheringDataChildList = arrayListOf<String>()
gatheringDataChildList.add("Record data: video + accelerometer")
val simulateChildList = arrayListOf<String>()
simulateChildList.add("Driving simulator")
listDataChild.put(listDataHeader[0], gatheringDataChildList)
listDataChild.put(listDataHeader[1], simulateChildList)
}
那麼,你究竟想要得到填充?有色物品還是別的東西?意味着你的觀點在活動中是空的? –
@AjayPandya是的。我的標題和子項都不顯示。視圖是完全空的。 – lidkxx
@lidkxx是'expandable_list''RecyclerView'?如果是這樣,你需要指定'layoutManager'(最有可能的'LinearLayoutManager') – rafal