zawadz88 / MaterialPopupMenu

Shows Material popup menus grouped in sections & more
Apache License 2.0
646 stars 59 forks source link

How to create menu from dynamic array? #30

Closed Pirokar closed 6 years ago

Pirokar commented 6 years ago

Looks like you allows only static way to create views.

zawadz88 commented 6 years ago

Hi @Pirokar, Could you provide a bit more context, please? How would you like for this to work exactly? If you have an array of items you would like to iterate over, you can add an item for each of them dynamically I guess. This should be similar to one of the samples in the sample app: see onConditionalItemsClicked in DarkActivity or LightActivity. There's an if statement there but this could easily be e.g. a for-each loop.

Pirokar commented 6 years ago

@zawadz88 this allows you to check that "if we have "A" item in array, than add "A". I want to write:

val section = MaterialPopupMenuBuilder.SectionHolder()
                for(menuListItem in listToImplement) {
                    section.customItem {
                        layoutResId = R.layout.item_filters_menu
                        viewBoundCallback = { view ->
                            val imageView = view.findViewById<ImageView>(R.id.filterImageFilters)
                            val textView = view.findViewById<TextView>(R.id.itemTextFilters)
                            val countView = view.findViewById<TextView>(R.id.communityCountTextFilters)
                            imageView.setImageResource(menuListItem.imageResourceId)
                            countView.text = menuListItem.ticketsCount.toString()
                            menuListItem.type.getTextForEnum()
                        }
                        callback = {
                            toast("Clicked!")
                        }
                    }
                }

                val builder = MaterialPopupMenuBuilder()
                builder.addSection(section)
                openPopupMenu = builder.build()

But this is impossible with current tools. I'm already connect your library as a module from your sourceCode and all that I need to make it work it's add this method:

fun addSection(sectionHolder: SectionHolder) {
        sectionHolderList.add(sectionHolder)
}
MFlisar commented 6 years ago

You can do following e.g.:

section {
    if (true) {
        item {
            label = "Dynamic item based on bool"
        }
    }
    for (i in 0..10) {
        item {
            label = "Dynamic item $i"
        }
    }
    item {
        label = "Static item"
    }
}

And similar. Was looking for the same like you and came to this solution...

zawadz88 commented 6 years ago

Thanks @MFlisar!