ViewGroupKt

Added in 1.1.0

public final class ViewGroupKt


Summary

Public methods

static final boolean
contains(@NonNull ViewGroup receiver, @NonNull View view)

Returns true if view is found in this view group.

static final void
forEach(
    @NonNull ViewGroup receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action on each view in this view group.

static final void
forEachIndexed(
    @NonNull ViewGroup receiver,
    @NonNull Function2<@NonNull Integer, @NonNull ViewUnit> action
)

Performs the given action on each view in this view group, providing its sequential index.

static final @NonNull View
get(@NonNull ViewGroup receiver, int index)

Returns the view at index.

static final @NonNull Sequence<@NonNull View>

Returns a Sequence over the immediate child views in this view group.

static final @NonNull Sequence<@NonNull View>

Returns a Sequence over the child views in this view group recursively.

static final @NonNull IntRange

Returns an IntRange of the valid indices for the children of this view group.

static final int

Returns the number of views in this view group.

static final boolean

Returns true if this view group contains no views.

static final boolean

Returns true if this view group contains one or more views.

static final @NonNull Iterator<@NonNull View>

Returns a MutableIterator over the views in this view group.

static final void

Removes view from this view group.

static final void
plusAssign(@NonNull ViewGroup receiver, @NonNull View view)

Adds view to this view group.

static final void

Sets the margins in the ViewGroup's MarginLayoutParams.

static final void
updateMargins(
    @NonNull ViewGroup.MarginLayoutParams receiver,
    @Px int left,
    @Px int top,
    @Px int right,
    @Px int bottom
)

Updates the margins in the ViewGroup's ViewGroup.MarginLayoutParams.

static final void
updateMarginsRelative(
    @NonNull ViewGroup.MarginLayoutParams receiver,
    @Px int start,
    @Px int top,
    @Px int end,
    @Px int bottom
)

Updates the relative margins in the ViewGroup's MarginLayoutParams.

Public methods

contains

public static final boolean contains(@NonNull ViewGroup receiver, @NonNull View view)

Returns true if view is found in this view group.

forEach

public static final void forEach(
    @NonNull ViewGroup receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action on each view in this view group.

forEachIndexed

public static final void forEachIndexed(
    @NonNull ViewGroup receiver,
    @NonNull Function2<@NonNull Integer, @NonNull ViewUnit> action
)

Performs the given action on each view in this view group, providing its sequential index.

get

public static final @NonNull View get(@NonNull ViewGroup receiver, int index)

Returns the view at index.

Throws
kotlin.IndexOutOfBoundsException

if index is less than 0 or greater than or equal to the count.

getChildren

public static final @NonNull Sequence<@NonNull ViewgetChildren(@NonNull ViewGroup receiver)

Returns a Sequence over the immediate child views in this view group.

getDescendants

public static final @NonNull Sequence<@NonNull ViewgetDescendants(@NonNull ViewGroup receiver)

Returns a Sequence over the child views in this view group recursively.

This performs a depth-first traversal. A view with no children will return a zero-element sequence.

For example, to efficiently filter views within the hierarchy using a predicate:

fun ViewGroup.findViewTreeIterator(predicate: (View) -> Boolean): Sequence<View> {
return sequenceOf(this)
.plus(descendantsTree)
.filter { predicate(it) }
}

getIndices

public static final @NonNull IntRange getIndices(@NonNull ViewGroup receiver)

Returns an IntRange of the valid indices for the children of this view group.

This can be used for looping:

for (i in viewGroup.indices.reversed) {
if (viewGroup[i] is SomeView) {
viewGroup.removeViewAt(i)
}
}

Or to determine if an index is valid:

if (2 in viewGroup.indices) {
// Do something…
}

getSize

public static final int getSize(@NonNull ViewGroup receiver)

Returns the number of views in this view group.

isEmpty

public static final boolean isEmpty(@NonNull ViewGroup receiver)

Returns true if this view group contains no views.

isNotEmpty

public static final boolean isNotEmpty(@NonNull ViewGroup receiver)

Returns true if this view group contains one or more views.

iterator

public static final @NonNull Iterator<@NonNull Viewiterator(@NonNull ViewGroup receiver)

Returns a MutableIterator over the views in this view group.

minusAssign

public static final void minusAssign(@NonNull ViewGroup receiver, @NonNull View view)

Removes view from this view group.

plusAssign

public static final void plusAssign(@NonNull ViewGroup receiver, @NonNull View view)

Adds view to this view group.

setMargins

public static final void setMargins(@NonNull ViewGroup.MarginLayoutParams receiver, @Px int size)

Sets the margins in the ViewGroup's MarginLayoutParams. This version of the method sets all axes to the provided size.

See also
setMargins

updateMargins

public static final void updateMargins(
    @NonNull ViewGroup.MarginLayoutParams receiver,
    @Px int left,
    @Px int top,
    @Px int right,
    @Px int bottom
)

Updates the margins in the ViewGroup's ViewGroup.MarginLayoutParams. This version of the method allows using named parameters to just set one or more axes.

See also
setMargins

updateMarginsRelative

public static final void updateMarginsRelative(
    @NonNull ViewGroup.MarginLayoutParams receiver,
    @Px int start,
    @Px int top,
    @Px int end,
    @Px int bottom
)

Updates the relative margins in the ViewGroup's MarginLayoutParams. This version of the method allows using named parameters to just set one or more axes.

Note that this inline method references platform APIs added in API 17 and may raise runtime verification warnings on earlier platforms. See Chromium's guide to Class Verification Failures for more information.

See also
setMargins