API Reference / Android Widgets / Hierarchical Menu
Apr. 24, 2019

Hierarchical Menu

About this widget

Hierarchical Menu is a filtering view that displays a hierarchy of facets which lets the user refine the search results.

To add a hierarchical menu to your search experience, use these components:

  • Searcher: The Searcher that handles your searches.
  • FilterState: The current state of the filters.
  • HierarchicalViewModel: The logic applied to the hierarchical facets.
  • HierarchicalView: The conrete hierarchical view.
  • HierarchicalPresenter: The presenter that controls the sorting and other settings of the hierarchical menu.
  • HierarchicalItem: A single node in the hierarchy.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@Ignore
class DocHierarchicalMenu {

    class HierarchicalViewHolder(val view: View) : RecyclerView.ViewHolder(view) {

        fun bind(item: HierarchicalItem, onClick: View.OnClickListener) {
            // Bind your view
        }
    }

    class HierarchicalAdapter : ListAdapter<HierarchicalItem, HierarchicalViewHolder>(diffUtil), HierarchicalView {

        override var onSelectionChanged: Callback<String>? = null

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HierarchicalViewHolder {
            return HierarchicalViewHolder(View(parent.context))
        }

        override fun onBindViewHolder(holder: HierarchicalViewHolder, position: Int) {
            val item = getItem(position)

            holder.bind(item, View.OnClickListener { onSelectionChanged?.invoke(item.facet.value) })
        }

        override fun setTree(tree: List<HierarchicalItem>) {
            submitList(tree)
        }

        companion object {

            private val diffUtil = object : DiffUtil.ItemCallback<HierarchicalItem>() {

                override fun areItemsTheSame(oldItem: HierarchicalItem, newItem: HierarchicalItem): Boolean {
                    return oldItem == newItem
                }

                override fun areContentsTheSame(oldItem: HierarchicalItem, newItem: HierarchicalItem): Boolean {
                    return oldItem == newItem
                }
            }
        }
    }

    class MyActivity : AppCompatActivity() {

        val client = ClientSearch(
            ApplicationID("YourApplicationID"),
            APIKey("YourAPIKey")
        )
        val index = client.initIndex(IndexName("YourIndexName"))
        val searcher = SearcherSingleIndex(index)
        val filterState = FilterState()

        val hierarchicalCategory = Attribute("hierarchicalCategories")
        val hierarchicalCategoryLvl0 = Attribute("$hierarchicalCategory.lvl0")
        val hierarchicalCategoryLvl1 = Attribute("$hierarchicalCategory.lvl1")
        val hierarchicalCategoryLvl2 = Attribute("$hierarchicalCategory.lvl2")
        val hierarchicalAttributes = listOf(
            hierarchicalCategoryLvl0,
            hierarchicalCategoryLvl1,
            hierarchicalCategoryLvl2
        )
        val separator = " > "
        val viewModel = HierarchicalViewModel(hierarchicalAttributes, separator)
        val connection = ConnectionHandler()

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

            val view = HierarchicalAdapter()

            connection += searcher.connectFilterState(filterState)
            connection += viewModel.connectFilterState(filterState)
            connection += viewModel.connectSearcher(searcher)
            connection += viewModel.connectView(view, HierarchicalPresenterImpl(separator))

            searcher.searchAsync()
        }

        override fun onDestroy() {
            super.onDestroy()
            searcher.cancel()
            connection.disconnect()
        }
    }
}

Parameters

hierarchicalAttributes
type: [Attribute]
Required

The names of the hierarchical attributes that we need to target, in ascending order.

1
2
3
4
5
6
7
  val hierarchicalAttributes = listOf(
    Attribute("hierarchicalCategories.lvl0"),
    Attribute("hierarchicalCategories.lvl1"),
    Attribute("hierarchicalCategories.lvl2"),
  )

  HierarchicalViewModel(hierarchicalAttributes)
separator
type: String
Required

The string separating the facets in the hierarchical facets. Usually something like “ > “. Note that you should not forget the spaces in between if there are some in your separator.

1
2
3
4
val hierarchicalAttributes = listOf(...)
val separator = " > "

HierarchicalViewModel(hierarchicalAttributes, separator)

Presenter

Hierarchical Presenter
type: HierarchicalPresenter
Required

The presenter that defines the way we want to display the list of HierarchicalItem.

Takes a list of HierarchicalItem as input and returns a new list of HierarchicalItem.

A HierarchicalItem contains a Facet, its level and a displayName.

1
2
3
val separator = " > "

HierarchicalPresenterImpl(separator)

Did you find this page helpful?