首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExpandableListAdapter显示错误的childViews

ExpandableListAdapter显示错误的childViews
EN

Stack Overflow用户
提问于 2021-02-09 00:36:38
回答 1查看 42关注 0票数 0

我正在编写一个应用程序来显示毒品组、毒品及其信息的树状视图。本质上,它是一个药物组的ExpandableListView,它在孩子们查看时显示单个药物的名称,并在点击时打开一个包含更多信息的新片段。

我坚持用正确的数据填充子视图。适配器似乎正确地获取了组数据,并且从日志记录和调试中,似乎也正确地传递了子数据。然而,ExpandableListView中childViews中的文本只对我打开的第一个组是正确的,下一个组显示的内容似乎是随机的(打开的顺序并不重要)。childViews的数量是正确的。详细信息视图(onClick)显示正确的信息,按下back按钮后,菜单将显示正确的信息(但是,任何新打开的组仍然显示错误的内容)。

我已经做了至少20轮检查和澄清任何可疑的代码,但都无济于事。

需要澄清的截图:

list view with two groups expanded

detail view, showing correct info (but not matching that shown in list view)

list view upon returning (notice contents now shown correctly)

下面是ExpandableListAdapter:

代码语言:javascript
复制
class MedicationsListAdapter(
    private val context: Context,
    private val groupList: List<String>,
    private val itemList: List<List<String>>
) : BaseExpandableListAdapter() {

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

    override fun getChildrenCount(groupPosition: Int): Int {
        return itemList[groupPosition].size
    }

    override fun getGroup(groupPosition: Int): List<String> {
        return itemList[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): String {
        return itemList[groupPosition][childPosition]
    }

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

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

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?,
    ): View {
        var groupView = convertView
        if (groupView == null) {
            val layoutInflater =
                this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            groupView = layoutInflater.inflate(R.layout.medication_list_group, null)

            val groupTextView: TextView = groupView.findViewById(R.id.text_group_name)
            groupTextView.text = groupList[groupPosition]
        } else return groupView
        return groupView
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        var childView = convertView
        if (childView == null) {
            val layoutInflater =
                this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            childView = layoutInflater.inflate(R.layout.medication_list_item, null)

            childView.findViewById<TextView>(R.id.text_medication_name).text = getChild(groupPosition, childPosition)

        } else return childView
        return childView
    }

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

以下是详细视图片段:

代码语言:javascript
复制
class MedicationItemFragment : Fragment() {

    private lateinit var medicationName: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //get medication name from SafeArgs
        arguments?.let {
            medicationName = it.getString("medicationName").toString()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_medication_item, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // get the correct medication data
        val medication: Medication = MedicationsListData().getMedication(medicationName)

        // populate the view with current medication's data
        view.findViewById<TextView>(R.id.text_pharmacodynamics_body).text =
            medication.pharmacodynamics
        view.findViewById<TextView>(R.id.text_contraindications_body).text =
            medication.contraindications
    }

    companion object {
        fun newInstance(): ParametersFragment = ParametersFragment()
    }
}

下面是提供适配器数据的类:

代码语言:javascript
复制
class GroupListData {
    fun getItemData(): List<List<String>> {
        return listOf(
            listOf("amoxicillin + clavulanate","penicillin","clindamycin","vancomycin"),
            listOf("epinephrine","norepinephrine","dopamine"),
            listOf("metoprolol","adenosine","amiodarone"),
            listOf("metoclopramide")
        )
    }

    fun getGroupData(): List<String> {
        return listOf(
            "antibiotics",
            "vasopressors",
            "antiarrhythmics",
            "antiemetics"
        )
    }
}

如果有必要,我可以详细说明或解释任何事情。任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-10 00:55:29

在四处阅读后,我现在了解到问题是回收视图没有正确填充。解决这个问题的方法是停止循环使用视图,而是为每个孩子创建一个新的视图:

代码语言:javascript
复制
    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        var childView = convertView
        val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        childView = layoutInflater.inflate(R.layout.medication_list_item, null)

        val medicationName = GroupListData().getItemData()[groupPosition][childPosition]
        val medication: Medication = MedicationsListData().getMedication(medicationName)

        childView.findViewById<TextView>(R.id.text_medication_name).text = medication.medicationName
        if(medication.brandName != "null") {
            childView.findViewById<TextView>(R.id.text_brand_name).text = medication.brandName
        }

        return childView
    }

我不会确切地认为这是一种解决方案,因为(非回收的)视图的数量可能会导致一些性能问题。但是,如果其他任何人都在为同样的问题而苦苦挣扎(并且没有预料到性能问题),那么这个方法是可行的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66105621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档