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
}
}
My issue is, As you can see in the fragment class, i have some group items, that doesnt have any child item. When i run it and click on the group, its showing an empty child item. I dont need that empty child item. i need to hide it. I tried some stuffs, but that doesn't work.
Any help would be deeply appreciated. Thanks in advance
Below is the Screenshot of issue i am facing,
I dont need that empty child item. i need to hide it. I tried some stuffs, but that doesn't work.
You might want to explain what stuffs you tried.
You are getting exactly what you asked for:
val parentAll = arrayListOf("")
val parentCereals = arrayListOf("")
val parentPoultry = arrayListOf("")
All, Cereals, and Poultry each have exactly one item: the empty string. If you don't want an empty string, don't add an empty string.
val parentAll = arrayListOf<String>() // OR emptyList<String>()
val parentCereals = arrayListOf<String>() // OR emptyList<String>()
val parentPoultry = arrayListOf<String>() // OR emptyList<String>()