Android 如何使用类别/子类别创建动态ExpandedListView或RecyclerView?
许多创建ExpandedListView的示例演示了如何创建已填充的ExpandedListView,而不是空的ExpandedListView。在同样的例子中,我们已经添加了类别(父类),并在其中添加了元素(子类)。但我需要能够按以下顺序添加:首先我添加一个类别(父类),然后我向该类别(子类)添加一个项目,它应该立即动态显示在屏幕上,而不是在关闭类别之后。添加n个元素,关闭类别。然后,如果需要,再添加一个,等等。 所有示例实现的问题在于,我们在一个列表或类似的东西中添加子元素,最后“关闭”子元素列表并将HashMap提供给适配器 我做了什么:标题(父项,类别)添加,动态显示。添加主体元素,但它们不会动态显示,若我尝试扩展类别,这将使应用程序崩溃。在类别关闭后(将所有儿童设置为列表),我可以展开类别并查看其中的所有儿童。使用RecyclerView可能更容易实现这一点?我不知道,像一个简单的类,有名称字段(header)和元素列表(child) 我的应用程序正在捕获BroadCastReceiver消息,我的应用程序有一个状态标志,如果应用程序的状态为“setParent”,则出现的消息,创建一个“Category”并成为其名称,然后程序进入状态“setChild”,以下消息被设置为元素(子元素)。如果消息值等于“Category.value”,则类别将关闭,应用程序将返回“setParent”状态 类文档活动:AppCompatActivity(){ 伴星{ var barcodeVal=“空” } 变量appStatus:appStatus=appStatus.setCell var openCategoryCode:String=“” //解释视图 val标题:MutableList=ArrayList() val body:MutableList=ArrayList() 变量listAdapter:MyListAdapter?=null val productList:MutableList=ArrayList() private val customBroadcastReceiver=customBroadcastReceiver() 重写创建时的乐趣(savedInstanceState:Bundle?){ super.onCreate(savedInstanceState) setContentView(com.bcr.app.R.layout.activity\u文档) registerReceiver(广播接收器、意图过滤器(“消息已接收”)) val expListView=findViewById(com.bcr.app.id.expandableListView)作为expandableListView setAdapter(ExpandableListAdapter(这个,expandableListView,头,体)) } var broadcastReceiver:broadcastReceiver=对象:broadcastReceiver(){ 覆盖接收(上下文:上下文,意图:意图){ Toast.makeText(applicationContext,“Catch”,Toast.LENGTH\u SHORT.show()) //检查传入消息 checkMessage() } } 有趣的消息(){ if(appStatus==appStatus.setChild&&openCategoryCode==barcodeVal){ Toast.makeText(此“类别已关闭”,Toast.LENGTH\u SHORT.show() appStatus=appStatus.setParent body.add(产品列表) listAdapter?.notifyDataSetChanged() } else if(appStatus==appStatus.setChild&&openCategoryCode!=barcodeVal){ setChild(barcodeVal) } else if(appStatus==appStatus.setParent&&openCategoryCode!=barcodeVal){ Toast.makeText(此“单元格已保存”,Toast.LENGTH\u SHORT.show()) openCellCode=barcodeVal appStatus=appStatus.setChild setParent(barcodeVal) } } fun setParent(barCodeVal:String){ 试一试{ header.add(barcodeVal) listAdapter?.notifyDataSetChanged() }捕获(例如:异常){ 例如printStackTrace() Toast.makeText(applicationContext,例如toString(),Toast.LENGTH\u SHORT.show()) } } fun setChild(barCodeVal:String){ 试一试{ productList.add(产品名称) listAdapter?.notifyDataSetChanged() }捕获(例如:异常){ 例如printStackTrace() Toast.makeText(applicationContext,例如toString(),Toast.LENGTH\u SHORT.show()) } }} 适配器Android 如何使用类别/子类别创建动态ExpandedListView或RecyclerView?,android,kotlin,arraylist,broadcastreceiver,expandablelistview,Android,Kotlin,Arraylist,Broadcastreceiver,Expandablelistview,许多创建ExpandedListView的示例演示了如何创建已填充的ExpandedListView,而不是空的ExpandedListView。在同样的例子中,我们已经添加了类别(父类),并在其中添加了元素(子类)。但我需要能够按以下顺序添加:首先我添加一个类别(父类),然后我向该类别(子类)添加一个项目,它应该立即动态显示在屏幕上,而不是在关闭类别之后。添加n个元素,关闭类别。然后,如果需要,再添加一个,等等。 所有示例实现的问题在于,我们在一个列表或类似的东西中添加子元素,最后“关闭”子元
class ExpandableListAdapter(var context : Context, var expandableListView: ExpandableListView, var header : MutableList<String>, var body : MutableList<MutableList<String>>) :
BaseExpandableListAdapter() {
override fun getGroup(groupPosition: Int): String {
return header[groupPosition]
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
override fun hasStableIds(): Boolean {
return false
}
override fun getGroupView(
groupPosition: Int,
isExpanded: Boolean,
convertView: View?,
parent: ViewGroup?
): View? {
var convertView = convertView
if(convertView == null){
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.layout_group, null)
}
val title = convertView?.findViewById<TextView>(R.id.textGroup)
title?.text = getGroup(groupPosition)
title?.setOnClickListener {
if (expandableListView.isGroupExpanded(groupPosition))
expandableListView.collapseGroup(groupPosition)
else
expandableListView.expandGroup(groupPosition)
//Toast.makeText(context, getGroup(groupPosition), Toast.LENGTH_SHORT).show()
}
return convertView
}
override fun getChildrenCount(groupPosition: Int): Int {
return body[groupPosition].size
}
override fun getChild(groupPosition: Int, childPosition: Int): String {
return body[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? {
var convertView = convertView
if(convertView == null){
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.layout_child, null)
}
val title = convertView?.findViewById<TextView>(R.id.textChild)
title?.text = getChild(groupPosition, childPosition)
title?.setOnClickListener {
//Toast.makeText(context, getChild(groupPosition, childPosition), Toast.LENGTH_SHORT).show()
}
return convertView
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long {
return childPosition.toLong()
}
override fun getGroupCount(): Int {
return header.size
}}
class ExpandableListAdapter(变量上下文:上下文,变量expandableListView:expandableListView,变量头:MutableList,变量正文:MutableList):
BaseExpandableListAdapter(){
重写fun getGroup(groupPosition:Int):字符串{
返回标题[groupPosition]
}
重写fun isChildSelectable(groupPosition:Int,childPosition:Int):布尔值{
返回真值
}
重写表ID():布尔值{
返回错误
}
覆盖getGroupView(
groupPosition:Int,
isExpanded:Boolean,
转换视图:视图?,
父:视图组?
):查看{
var convertView=convertView
if(convertView==null){
val inflater=context.getSystemService(context.LAYOUT\u inflater\u SERVICE)作为LayoutInflater
convertView=充气机。充气(R.layout.layout_组,空)
}
val title=convertView?.findViewById(R.id.textGroup)
title?.text=getGroup(groupPosition)
标题?.setOnClickListener{
if(expandableListView.isGroupExpanded(groupPosition))
expandableListView.collapseGroup(组位置)
其他的
expandableListView.expandGroup(groupPosition)
//Toast.makeText(context,getGroup(groupPosition),Toast.LENGTH\u SHORT.show()
}
返回转换视图
}
覆盖趣味getChildrenCount(groupPosition:Int):Int{
返回正文[groupPosition]。大小
}
重写fun getChild(groupPosition:Int,childPosition:Int):字符串{
返回主体[groupPosition][childPosition]
}
覆盖有趣的getGroupId(groupPosition:Int):长{
returngroupposition.toLong()
}
覆盖getChildView(
groupPosition:Int,
childPosition:Int,
isLastChild:Boolean,
转换视图:视图?,
父:视图组?
):查看{
var convertView=convertView
if(convertView==null){
val inflater=context.getSystemService(context.LAYOUT\u inflater\u SERVICE)作为LayoutInflater
convertView=充气机。充气(R.layout.layout_子项,空)
}
val title=convertView?.findViewB
class ExpandableListAdapter(var context : Context, var expandableListView: ExpandableListView, var header : MutableList<String>, var body : MutableList<MutableList<String>>) :
BaseExpandableListAdapter() {
override fun getGroup(groupPosition: Int): String {
return header[groupPosition]
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
override fun hasStableIds(): Boolean {
return false
}
override fun getGroupView(
groupPosition: Int,
isExpanded: Boolean,
convertView: View?,
parent: ViewGroup?
): View? {
var convertView = convertView
if(convertView == null){
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.layout_group, null)
}
val title = convertView?.findViewById<TextView>(R.id.textGroup)
title?.text = getGroup(groupPosition)
title?.setOnClickListener {
if (expandableListView.isGroupExpanded(groupPosition))
expandableListView.collapseGroup(groupPosition)
else
expandableListView.expandGroup(groupPosition)
//Toast.makeText(context, getGroup(groupPosition), Toast.LENGTH_SHORT).show()
}
return convertView
}
override fun getChildrenCount(groupPosition: Int): Int {
return body[groupPosition].size
}
override fun getChild(groupPosition: Int, childPosition: Int): String {
return body[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? {
var convertView = convertView
if(convertView == null){
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.layout_child, null)
}
val title = convertView?.findViewById<TextView>(R.id.textChild)
title?.text = getChild(groupPosition, childPosition)
title?.setOnClickListener {
//Toast.makeText(context, getChild(groupPosition, childPosition), Toast.LENGTH_SHORT).show()
}
return convertView
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long {
return childPosition.toLong()
}
override fun getGroupCount(): Int {
return header.size
}}