Skip to content

Commit 8d044c4

Browse files
pubiqqpekingme
authored andcommitted
[HideViewOnScrollBehavior] Add a method to get the HideViewOnScrollBehavior associated with the view
Resolves #4768 GIT_ORIGIN_REV_ID=9471abba29b6c63aaeadf6cb4708480e8e8cc0b8 PiperOrigin-RevId: 757874866
1 parent 8ec6b77 commit 8d044c4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lib/java/com/google/android/material/behavior/HideViewOnScrollBehavior.java

+22
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,26 @@ public void disableOnTouchExploration(boolean disableOnTouchExploration) {
421421
public boolean isDisabledOnTouchExploration() {
422422
return disableOnTouchExploration;
423423
}
424+
425+
/**
426+
* A utility function to get the {@link HideViewOnScrollBehavior} associated with the {@code
427+
* view}.
428+
*
429+
* @param view The {@link View} with {@link HideViewOnScrollBehavior}.
430+
* @return The {@link HideViewOnScrollBehavior} associated with the {@code view}.
431+
*/
432+
@NonNull
433+
@SuppressWarnings("unchecked")
434+
public static <V extends View> HideViewOnScrollBehavior<V> from(@NonNull V view) {
435+
ViewGroup.LayoutParams params = view.getLayoutParams();
436+
if (!(params instanceof LayoutParams)) {
437+
throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
438+
}
439+
CoordinatorLayout.Behavior<?> behavior = ((LayoutParams) params).getBehavior();
440+
if (!(behavior instanceof HideViewOnScrollBehavior)) {
441+
throw new IllegalArgumentException(
442+
"The view is not associated with HideViewOnScrollBehavior");
443+
}
444+
return (HideViewOnScrollBehavior<V>) behavior;
445+
}
424446
}

0 commit comments

Comments
 (0)