ExpandedListView:如何使用List

问题描述 投票:0回答:2
expandGroup或获取正确的groupPosition

在此问题之前,我可以扩大小组。团体和儿童收藏是:

val header: MutableList<String> = ArrayList()
var productList : MutableList<String> = mutableListOf()
val body: MutableList<MutableList<String>> = mutableListOf()

之后,我需要向标题和子项(productList)添加更多数据。所以我创建了两个模型类:

data class Cell (var barcode: String = "", var name: String = "")

data class Product(
    var barcode: String = "",
    var id: String = "",
    var name: String = "",
    var time: String = "",
    var date: String = "",
    var weight: Int = 0
)

并将我在“活动”中的收藏更改为:

var header: MutableList<Cell> = mutableListOf()
var productList : MutableList<Product> = mutableListOf()
val body: MutableList<MutableList<Product>> = mutableListOf()

我的适配器是:

class ExpandableListAdapter(var context : Context, var expandableListView: ExpandableListView, var header : MutableList<Cell>, var body: MutableList<MutableList<Product>>) :
    BaseExpandableListAdapter() {

    override fun getGroup(groupPosition: Int): String {
        return header[groupPosition].toString()
    }

    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.textGroupCode)
        title?.text = getGroup(groupPosition)
        val det = header.get(groupPosition)
        title?.setText(det.name.toString())

        title?.setOnClickListener {
            if (expandableListView.isGroupExpanded(groupPosition))
                expandableListView.collapseGroup(groupPosition)
            else
                expandableListView.expandGroup(groupPosition)
            Toast.makeText(context, getGroup(groupPosition).toString(), Toast.LENGTH_SHORT).show()
        }
        return convertView
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return body[groupPosition].size
        //return header.get(groupPosition).getChildList().size()
    }

    override fun getChild(groupPosition: Int, childPosition: Int): String {
        return body[groupPosition][childPosition].toString()
        //return header.get(groupPosition).get(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.textChildId)
        title?.text = getChild(groupPosition, childPosition)
        val name = convertView?.findViewById<TextView>(R.id.textChildName)
        val date = convertView?.findViewById<TextView>(R.id.textChildDate)
        val time = convertView?.findViewById<TextView>(R.id.textChildTime)
        val weight = convertView?.findViewById<TextView>(R.id.textChildWeight)

        val det = body.get(groupPosition).get(childPosition)
        title?.setText(det.id.toString())
        name?.setText(det.name.toString())
        date?.setText(det.date.toString())
        time?.setText(det.time.toString())
        weight?.setText(det.weight.toString())

        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
    }
}

现在,当我尝试扩展/单击我的小组或孩子时,我得到了他所有字段值的课程。我没有获得元素的位置。如何解决这个问题?如何使用班级清单并获得正确的位置???

在此问题之前,我可以扩大小组。组和子集合为:val标头:MutableList = ArrayList()var productList:MutableList = mutableListOf()val主体:...

android list object kotlin expandablelistview
2个回答
0
投票

请尝试这个,我有打印组位置,子位置和处理支出事件。

     class ExpendNewListAdapter(
               var mContext: Context,
                       var  mListDataHeader: List<String>?,
                       var mListDataChild: HashMap<String, List<EventsItem>?>?) : BaseExpandableListAdapter() {
override fun getChild(groupPosition: Int, childPosititon: Int): Any {
    return this.mListDataChild?.get(this.mListDataHeader?.get(groupPosition))!!.get(childPosititon)
}

override fun getChildId(groupPosition: Int, childPosition: Int): Long {
    return childPosition.toLong()
}

override fun getChildView(groupPosition: Int, childPosition: Int,
                          isLastChild: Boolean, convertView: View?, parent: ViewGroup): View {


    val events = getChild(groupPosition, childPosition) as EventsItem

    val infalInflater = this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
      val  childView = infalInflater.inflate(R.layout.list_item_expandable, null)

    val txtListEventName = childView
            .findViewById<View>(R.id.text_event_name) as TextView

    Log.e("child Position",childPosition.toString())

    return childView
}

override fun getChildrenCount(groupPosition: Int): Int {
    return this.mListDataChild?.get(this.mListDataHeader?.get(groupPosition))?.size ?:0
}

override fun getGroup(groupPosition: Int): String? {
    return this.mListDataHeader?.get(groupPosition)
}

override fun getGroupCount(): Int {
    return this.mListDataHeader?.size?:0
}

override fun getGroupId(groupPosition: Int): Long {
    return groupPosition.toLong()
}

override fun getGroupView(groupPosition: Int, isExpanded: Boolean,
                          convertView: View?, parent: ViewGroup): View {
    val infalInflater = this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
     val headerView = infalInflater.inflate(R.layout.expend_list_layout, null)

    val innerLayout = headerView
            .findViewById<View>(R.id.layout_inner) as ConstraintLayout


    val viewBottom = headerView
            .findViewById<View>(R.id.view_botttom_parent) as View
    Log.e("group Position",groupPosition.toString())
        if(isExpanded ) {
            innerLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.button_green))
            viewBottom.visibility = View.GONE
        }
        else
        {
            innerLayout.setBackgroundColor(ContextCompat.getColor(mContext,R.color.button_blue))
            viewBottom.visibility=View.VISIBLE
        }



    return headerView
}

override fun hasStableIds(): Boolean {
    return false
}

override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
    return true
}

0
投票

就我而言,我将所有事件设置为每月一次,其中组项目为月份,子项为我从响应中获取的所有事件。您可以根据需要进行更改。

© www.soinside.com 2019 - 2024. All rights reserved.