API Reference / Android Widgets / Filter Map

Filter Map

About this widget

Components holding a map of filters, and that can apply a single filter at a time.

To add a filter map to your search experience, you can use these components:

  • Searcher: the Searcher that handles searches.
  • FilterState: the current state of the filters.
  • FilterMapViewModel: the logic applied to the filter map.
  • FilterMapView: the view that renders the filter map.

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
class FilterMapViewRadioGroup(
    val radioGroup: RadioGroup
) : FilterMapView,
    RadioGroup.OnCheckedChangeListener {

    override var onSelectionChange: Callback<Int>? = null

    init {
        radioGroup.setOnCheckedChangeListener(this)
    }

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        onSelectionChange?.invoke(checkedId)
    }

    override fun setSelected(selected: Int?) {
        radioGroup.setOnCheckedChangeListener(null)
        radioGroup.check(selected ?: View.NO_ID)
        radioGroup.setOnCheckedChangeListener(this)
    }

    override fun setMap(map: Map<Int, String>) {
        for (index in 0 until radioGroup.childCount) {
            val view = radioGroup.getChildAt(index) as? RadioButton

            map[view?.id]?.let { view?.text = it }
        }
    }
}

class MyActivity : AppCompatActivity() {

    val gender = Attribute("gender")
    val filterState = FilterState()
    val client = ClientSearch(
        ApplicationID("YourApplicationID"),
        APIKey("YourAPIKey")
    )
    val index = client.initIndex(IndexName("YourIndexName"))
    val searcher = SearcherSingleIndex(index)
    val filters = mapOf(
        0 to Filter.Facet(gender, "male"),
        1 to Filter.Facet(gender, "female")
    )
    val viewModel = FilterMapViewModel(filters)
    val connection = ConnectionHandler()

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

        val radioGroup = RadioGroup(this)
        val view = FilterMapViewRadioGroup(radioGroup)

        connection += viewModel.connectFilterState(filterState)
        connection += viewModel.connectView(view)
        connection += viewModel.connectFilterState(filterState)

        searcher.searchAsync()
    }

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

Parameters

searcher
type: Searcher
Required

The Searcher that handles searches.

1
searcher.connectFilterState(filterState)
filterState
type: FilterState
Required

The FilterState that holds filters.

1
viewModel.connectFilterState(filterState)
view
type: FilterMapView
Required

The view that renders the filter map.

1
viewModel.connectView(view)
filters
type: Map<Int, Filter>
Required

The map of filters to be held by the ViewModel. The key is an unique identifier for the filter value.

1
2
3
4
5
6
val gender = Attribute("gender")
val filters = mapOf(
    0 to Filter.Facet(gender, "male"),
    1 to Filter.Facet(gender, "female")
)
val viewModel = FilterMapViewModel(filters)
groupID
type: FilterGroupID
default: FilterGroupID(attribute.raw)
Optional

Groups all created filters under an ID and composes them with this operator. Defaults to the used attribute, with FilterOperator.And between filters in this group.

1
2
3
4
val price = Attribute("price")
val groupID = FilterGroupID(price, FilterOperator.And)

viewModel.connectFilterState(price, filterState, groupID = groupID)

Did you find this page helpful?

InstantSearch Android v2