API Reference / Android Widgets / Filter List (Tag)
Apr. 24, 2019

Filter List (Tag)

About this widget

FilterList.Tag is a filtering view that displays any kind of tag filters and lets the user refine the search results by selecting them.

Compared to the RefinementList, which takes its values from the search response facets, this widget displays tag filters that you add yourself.

To add a filter list to your search experience, use these components:

  • Searcher: The Searcher that handles your searches.
  • FilterState: The current state of the filters.
  • FilterListViewModel.Tag: The logic applied to the tag filters.
  • FilterListView.Tag: The view that will render the tag filters.
  • FilterPresenter: Optional. The presenter to customize the display of the filters.

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
class MyActivity : AppCompatActivity() {

    val client = ClientSearch(
        ApplicationID("YourApplicationID"),
        APIKey("YourAPIKey")
    )
    val index = client.initIndex(IndexName("YourIndexName"))
    val searcher = SearcherSingleIndex(index)
    val filterState = FilterState()
    val filters = listOf(
        Filter.Tag("free shipping"),
        Filter.Tag("coupon"),
        Filter.Tag("free return"),
        Filter.Tag("on sale"),
        Filter.Tag("no exchange")
    )
    val viewModel = FilterListViewModel.Tag(filters)
    val connection = ConnectionHandler()

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

        val view: FilterListView.Tag = MyFilterListRecyclerViewAdapter()

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

        searcher.searchAsync()
    }

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

Parameters

searcher
type: Searcher
Required

The Searcher that handles your searches.

1
viewModel.connectSearcher(searcher)
filterState
type: FilterState
Required

The FilterState that will hold your filters.

1
viewModel.connectFilterState(filterState)
tagListView
type: FilterListView.Tag
Required

The view that will render the tag filters.

1
viewModel.connectView(tagListView)
attribute
type: Attribute
Required

The attribute to filter.

1
2
val attribute = Attribute("price")
viewModel.connectFilterState(filterState, attribute)
items
type: List<Filter.Tag>
default: listOf()
Required

The tag filters to display.

1
2
val items = listOf(Filter.Tag(Attribute("price"), 5..10))
FilterListViewModel.Tag(items = items)
groupId.operator
type: FilterOperator
default: FilterOperator.And
Optional

Whether we apply an And or Or behavior to the filters in the filterState.

For example if we have an or behavior, the filter sent to Algolia will be _tags:promo OR color:green.

1
viewModel.connectFilterState(filterState, FilterGroupID(FilterOperator.Or))
selectionMode
type: SelectionMode
default: Multiple
Optional

Whether the list can have Single or Multiple selections.

1
FilterListViewModel.Tag(items, selectionMode = SelectionMode.Multiple)

Presenter

presenter
type: FilterPresenter
default: FilterPresenterImpl()
Optional

A presenter describing how to display a filter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
val presenter = object : FilterPresenter {
    override val facetString: (Attribute, String, Boolean) -> String = { attribute, value, isNegated ->
        "$attribute: ${if (isNegated) "!" else ""}$value"
    }
    override val facetBoolean: (Attribute, Boolean, Boolean) -> String = { attribute, value, isNegated ->
        "$attribute: ${if (isNegated) "!" else ""}$value"
    }
    override val facetNumber: (Attribute, Number, Boolean) -> String = { attribute, value, isNegated ->
        "$attribute: ${if (isNegated) "!" else ""}$value"
    }
    override val tag: (Attribute, String, Boolean) -> String = { _, value, isNegated ->
        "tag: ${if (isNegated) "!" else ""}$value"
    }
    override val numericComparison: (Attribute, NumericOperator, Number, Boolean) -> String =
        { attribute, operator, value, isNegated ->
            "$attribute: ${if (isNegated) "!" else ""}$operator $value"
        }
    override val numericRange: (Attribute, Number, Number, Boolean) -> String =
        { attribute, lowerBound, upperBound, isNegated ->
            "${if (isNegated) "!" else ""}$lowerBound < $attribute <= $upperBound"
        }
}
viewModel.connectView(view, presenter)

Did you find this page helpful?