【发布时间】:2017-08-01 12:00:51
【问题描述】:
我正在尝试在我的应用中实现 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)
}
【问题讨论】:
-
那么您究竟想要填充什么?有色物品还是其他东西?意味着您的 lsitview 在活动中是空的?
-
@AjayPandya 是的。我的标题和子项都没有显示。视图完全是空的。
-
@lidkxx 是
expandable_listRecyclerView?如果是这样,您需要指定layoutManager(很可能是LinearLayoutManager)
标签: android list kotlin expandablelistview expandablelistadapter