如果父级在可扩展列表视图 Kotlin 中具有空子级,则删除子级视图

问题描述 投票:0回答:1
Adapter Class
------------------
class FilterByTypeExpandableListAdapter(val filterByData: (FilterByItemDataModel) -> Unit) : BaseExpandableListAdapter() {

    private lateinit var parentList: ArrayList<String>
    private lateinit var childList: HashMap<String, ArrayList<String>>

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

    override fun getChildrenCount(groupPosition: Int): Int {
        val childCount: Int = if (childList[parentList[groupPosition]]!!.size == 0) {
            0
        } else {
            childList[parentList[groupPosition]]!!.size
        }

        return childCount
    }

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

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return childList[parentList[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 false
    }

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View? {
        var convertView = convertView
        val parentItem = getGroup(groupPosition) as String

        if (convertView == null) {
            val layoutInflater =
                parent?.context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = layoutInflater.inflate(R.layout.sample_parent_list, parent, false)
        }
        val parentTitleTextView =
            convertView?.findViewById<AppCompatTextView>(R.id.sample_parent_item_txt)
        parentTitleTextView?.text = parentItem

        return convertView
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View? {
        var convertView = convertView
        val childItem = getChild(groupPosition, childPosition) as String

        if (convertView == null) {
            val layoutInflater =
                parent?.context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = layoutInflater.inflate(R.layout.sample_child_list, parent, false)
        }

        val childTitleLayout = convertView?.findViewById<ConstraintLayout>(R.id.child_item_layout)
        val childTitleTextView =
            convertView?.findViewById<AppCompatTextView>(R.id.sample_child_item_txt)

        if (childItem.isEmpty()) {
            childTitleLayout?.visibility = View.GONE
        } else {
            childTitleLayout?.visibility = View.VISIBLE
            childTitleTextView?.text = childItem
        }

        return convertView
    }

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

    fun setParentAndChildData(
        parentList: ArrayList<String>,
        childList: HashMap<String, ArrayList<String>>
    ) {
        this.parentList = parentList
        this.childList = childList
    }

}


Fragment class
--------------------
class SampleBottomSheetDialog(private val onClick: (ArrayList<String>) -> Unit) :
    BottomSheetDialogFragment() {

private lateinit var dialogLayoutBinding: DialogLayoutBinding

    private lateinit var parentList: ArrayList<String>
    private lateinit var childList: HashMap<String, ArrayList<String>>

   private val sampleTypeExpandableListAdapter by lazy {
        SampleExpandableListAdapter { action ->
            actionOnClick(
                action
            )
        }
    }


override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        dialogFilterLayoutBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context), R.layout.dialog_filter_layout, null, false
        )

        if (dialog != null && dialog?.window != null) {
            dialog?.let { dialog ->
                dialog.window?.let { window ->
                    window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                    window.requestFeature(Window.FEATURE_NO_TITLE)
                }
            }
        }

        childList = getTypeList()
        parentList = ArrayList(childList.keys)

        return dialogFilterLayoutBinding.root
    }


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

         setSampleDataExpandableListView(parentList, childList)
}


    private fun setSampleExpandableListView(
        parentList: ArrayList<String>,
        childList: HashMap<String, ArrayList<String>>
    ) {
        sampleExpandableListAdapter.setParentAndChildData(parentList, childList)
        dialogFilterLayoutBinding.sampleExpandableListView.setAdapter(
            sampleExpandableListAdapter
        )

        dialogFilterLayoutBinding.sampleExpandableListView.setOnGroupExpandListener { groupPosition ->
            Toast(requireActivity(), "List Expanded:: ${parentList[groupPosition]}")
        }

        dialogFilterLayoutBinding.sampleExpandableListView.setOnGroupCollapseListener { groupPosition ->
            Toast(requireActivity(), "List Collapsed:: ${parentList[groupPosition]}")
        }

        dialogFilterLayoutBinding.sampleExpandableListView.setOnChildClickListener { parent, v, groupPosition, childPosition, id ->
            Toast(
                requireActivity(),
                "ParentItem:: ${parentList[groupPosition]} and ChildItem:: ${childList[parentList[groupPosition]]!![childPosition]}"
            )
            false
        }
    }


    fun getTypeList(): HashMap<String, ArrayList<String>> {
        val sampleArrayList = HashMap<String, ArrayList<String>>()

        val parentAll = arrayListOf("")
        val parentFruits = arrayListOf("Apple", "Kiwi", "Orange")
        val parentCereals = arrayListOf("")
        val parentPoultry = arrayListOf("")
        val parentMyVeggies = arrayListOf("Cucumber", "Tomato")

        sampleArrayList["All"] = parentAll
        sampleArrayList["Fruits"] = parentFruits
        sampleArrayList["Cereals"] = parentCereals
        sampleArrayList["Poultry"] = parentPoultry
        sampleArrayList["Vegetables"] = parentMyVeggies

        return sampleArrayList
    }

}

我的问题是,正如您在片段类中看到的,我有一些组项目,没有任何子项目。当我运行它并单击该组时,它显示一个空的子项目。我不需要那个空的子项目。我需要隐藏它。我尝试了一些东西,但不起作用。

任何帮助将不胜感激。预先感谢

下面是我面临的问题的屏幕截图,

android kotlin android-recyclerview expandablelistview expandablelistadapter
1个回答
0
投票

我不需要那个空的子项目。我需要隐藏它。我尝试了一些东西,但不起作用。

您可能想解释一下您尝试过哪些东西。

您得到的正是您所要求的:

val parentAll = arrayListOf("")
val parentCereals = arrayListOf("")
val parentPoultry = arrayListOf("")

All、Cereals 和 Poultry 都只有一项:空字符串。如果您不需要空字符串,请不要添加空字符串。

val parentAll = arrayListOf<String>() // OR emptyList<String>()
val parentCereals = arrayListOf<String>() // OR emptyList<String>()
val parentPoultry = arrayListOf<String>() // OR emptyList<String>()
© www.soinside.com 2019 - 2024. All rights reserved.