WindowInsetsApplier


public final class WindowInsetsApplier implements OnApplyWindowInsetsListener


An OnApplyWindowInsetsListener that applies WindowInsets to all children of a ViewPager2, making sure they all receive the same insets regardless of whether any of them consumed any insets.

To prevent the ViewPager2 itself from dispatching the insets incorrectly, this listener will consume all insets it applies. As a consequence, siblings of ViewPager2, or siblings of its parents, to whom the WindowInsets haven't yet been dispatched, won't receive them at all. If you require those views to receive the WindowInsets, do not set this listener on ViewPager2 and do not consume insets in any of the pages.

Call install to install this listener in ViewPager2.

When running on API 30 or higher and the targetSdkVersion is set to API 30 or higher, the fix is not needed and install will do nothing. None of the above described effects will happen.

Summary

Public methods

static boolean

Installs a WindowInsetsApplier into the given ViewPager2, but only when window insets dispatching hasn't been fixed in the current run configuration.

@NonNull WindowInsetsCompat

When set on a View, this listener method will be called instead of the view's own onApplyWindowInsets method.

Public methods

install

Added in 1.1.0-beta03
public static boolean install(@NonNull ViewPager2 viewPager)

Installs a WindowInsetsApplier into the given ViewPager2, but only when window insets dispatching hasn't been fixed in the current run configuration. It will return whether or not the WindowInsetsApplier was installed.

Window insets dispatching is fixed on Android SDK R, but the targetSdk of the app also needs to be set to R or higher. If both these conditions hold, the WindowInsetsApplier won't be installed. If either we're running on SDK

Parameters
@NonNull ViewPager2 viewPager

The ViewPager2 to install the WindowInsetsApplier into

Returns
boolean

Whether or not the WindowInsetsApplier was installed

onApplyWindowInsets

Added in 1.1.0-beta03
public @NonNull WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets)

When set on a View, this listener method will be called instead of the view's own onApplyWindowInsets method.

Parameters
@NonNull View v

The view applying window insets

@NonNull WindowInsetsCompat insets

The insets to apply

Returns
@NonNull WindowInsetsCompat

The insets supplied, minus any insets that were consumed