AdaptiveFragmentHelper

@IviExperimental(reasons = [])
class AdaptiveFragmentHelper<T : Enum<T>>(fragment: Fragment, viewVariantSelector: (widthDp: Int, heightDp: Int) -> T, viewFactoryProvider: (T) -> IviFragment.ViewFactory<*>)

A helper class for fragments that change their view depending on the size that it's given, offering an adaptive UI.

A 'view variant' of type T defines the different possible variations of the fragment's view. For example the view could have different orientations (HORIZONTAL, VERTICAL), different sizes (SMALL, MEDIUM, LARGE), or any combination of those variations.

Each time the fragment's root view size change, a different view variant can be selected via viewVariantSelector. If a different view variant is selected, the fragment's view is recreated with the corresponding ViewFactory provided by the viewFactoryProvider.

To obtain the initial fragment's root view size, a stub view is created first. In Fragment.onViewCreated this can be checked with View.isAdaptiveStub.

Parameters

T

The enum type of view variant.

fragment

The adaptive fragment.

viewVariantSelector

Called when the fragment's root view updates its size in order to determine which view variant to use. If the returned value differs from the previous one the fragment will be reattached in order to recreate the view. In case the value to which the size is compared is stored in the dimen xml, one can use the Context.getDimensionByAttrInDp function. If a view or any of its parents has the layout param WRAP_CONTENT, the size can vary after the view variant is chosen, causing a new view variant to be selected, which may end up in an endless loop. To prevent this from happening, such a view will report the stable value 0 in the WRAP_CONTENT dimension.

viewFactoryProvider

Called when the size of the fragment's root view changes and a different view variant is returned by viewVariantSelector. This function should return the new value for viewFactory, which can be returned from IviFragment.viewFactory to apply the new view variant when the fragment recreates its view.

Constructors

Link copied to clipboard
constructor(fragment: Fragment, viewVariantSelector: (widthDp: Int, heightDp: Int) -> T, viewFactoryProvider: (T) -> IviFragment.ViewFactory<*>)

Properties

Link copied to clipboard

The IviFragment.ViewFactory that the fragment should use to create views. Can be used easily from the fragment with: