【问题标题】:why my getChildView() is getting called 2 times?为什么我的 getChildView() 被调用了 2 次?
【发布时间】:2020-01-08 16:47:13
【问题描述】:

我的组有 5 个项目,但它重复相同并变成 10 个,我不知道为什么。下面是我的适配器,到目前为止,fill_parent 或 match_parent 的任何解决方案都对我不起作用。

class FilterAdapter(私有验证上下文:上下文,私有验证团队名称:ArrayList): BaseExpandableListAdapter() {

override fun getChild(groupPosition: Int, childPosition: Int): Any {
    val productList = teamName[groupPosition].playerName
    return productList[childPosition]
}

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

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


    val detailInfo = getChild(groupPosition, childPosition) as ChildData
    if (view == null) {
        val infalInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        view = infalInflater.inflate(R.layout.item_layout_filter_child, null)
    }
    val childItem = view!!.findViewById<View>(R.id.childDetailItem) as TextView
    val linChildLayout = view.findViewById<View>(R.id.linFilterChild) as LinearLayout
    val filterCheckBox = view.findViewById<View>(R.id.filterCheckBox) as CheckBox
    val linCheckbox = view.findViewById<View>(R.id.linCheckbox) as LinearLayout


    childItem.text = detailInfo.name.trim { it <= ' ' }
    Log.e("item", "" + childItem.text)

    linChildLayout.setOnClickListener {
        //                Log.e("filterchildPosition","    "+detailInfo.getName()+" status is now "+filterCheckBox.isChecked());

        if (filterCheckBox.isChecked) {
            filterCheckBox.isChecked = false
            Log.e("filterchildPositionif", "    " + detailInfo.name + " status is now " + filterCheckBox.isChecked)
        } else {
            filterCheckBox.isChecked = true
            Log.e(
                "filterchildPositionelse",
                "    " + detailInfo.name + " status is now " + filterCheckBox.isChecked
            )
        }
    }

    filterCheckBox.setOnCheckedChangeListener { buttonView, isChecked ->
        if (filterCheckBox.isChecked) {
            // your code to checked checkbox
            Log.e("filterchildPositionif", "    " + detailInfo.name + " status is now " + filterCheckBox.isChecked)
        } else {
            // your code to  no checked checkbox
            Log.e(
                "filterchildPositionelse",
                "    " + detailInfo.name + " status is now " + filterCheckBox.isChecked
            )
        }
    }

    return view
}

override fun getChildrenCount(groupPosition: Int): Int {

    val productList = teamName[groupPosition].playerName
    Log.e("listsize",productList.size.toString())
    return productList.size

}

override fun getGroup(groupPosition: Int): Any {
    return teamName[groupPosition]
}

override fun getGroupCount(): Int {
    return teamName.size
}

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

override fun getGroupView(
    groupPosition: Int, isLastChild: Boolean, view: View?,
    parent: ViewGroup
): View {
    var view = view

    val headerInfo = getGroup(groupPosition) as GroupData
    if (view == null) {
        val inf = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        view = inf.inflate(R.layout.item_layout_filter_group, null)
    }

    val heading = view!!.findViewById<View>(R.id.heading) as TextView

    heading.text = headerInfo.name.trim { it <= ' ' }

    return view
}

override fun hasStableIds(): Boolean {
    return true
}

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

}

【问题讨论】:

  • 也许问题不在这里,请贴出使用它的代码。
  • 感谢@Augusto 的回复,这里是活动代码的链接。 justpaste.it/7ae2r

标签: android kotlin expandablelistadapter


【解决方案1】:

我所做的只是将 getChildrenCount 的计数减少到一半是重复的前半部分。

覆盖 fun getChildrenCount(groupPosition: Int): Int {

val productList = teamName[groupPosition].playerName
Log.e("listsize",productList.size.toString())
return productList.size

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2011-09-04
    • 2018-02-18
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多