Last active
May 22, 2024 02:59
-
-
Save BramYeh/96bebedce150eb4abcfa5f5c782871f6 to your computer and use it in GitHub Desktop.
ViewPagerBottomSheetBehavior overrides package level methods and fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package android.support.design.widget; | |
import android.support.v4.view.ViewCompat; | |
import android.support.v4.view.ViewPager; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import java.lang.ref.WeakReference; | |
/** | |
* Override {@link #findScrollingChild(View)} to support {@link ViewPager}'s nested scrolling. | |
* | |
* By the way, In order to override package level method and field. | |
* This class put in the same package path where {@link BottomSheetBehavior} located. | |
*/ | |
public class ViewPagerBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> { | |
@Override | |
View findScrollingChild(View view) { | |
if (ViewCompat.isNestedScrollingEnabled(view)) { | |
return view; | |
} | |
if (view instanceof ViewPager) { | |
ViewPager viewPager = (ViewPager) view; | |
View currentViewPagerChild = viewPager.getChildAt(viewPager.getCurrentItem()); | |
View scrollingChild = findScrollingChild(currentViewPagerChild); | |
if (scrollingChild != null) { | |
return scrollingChild; | |
} | |
} else if (view instanceof ViewGroup) { | |
ViewGroup group = (ViewGroup) view; | |
for (int i = 0, count = group.getChildCount(); i < count; i++) { | |
View scrollingChild = findScrollingChild(group.getChildAt(i)); | |
if (scrollingChild != null) { | |
return scrollingChild; | |
} | |
} | |
} | |
return null; | |
} | |
public void updateScrollingChild() { | |
final View scrollingChild = findScrollingChild(mViewRef.get()); | |
mNestedScrollingChildRef = new WeakReference<>(scrollingChild); | |
} | |
/** | |
* A utility function to get the {@link ViewPagerBottomSheetBehavior} associated with the {@code view}. | |
* | |
* @param view The {@link View} with {@link ViewPagerBottomSheetBehavior}. | |
* @return The {@link ViewPagerBottomSheetBehavior} associated with the {@code view}. | |
*/ | |
@SuppressWarnings("unchecked") | |
public static <V extends View> ViewPagerBottomSheetBehavior<V> from(V view) { | |
ViewGroup.LayoutParams params = view.getLayoutParams(); | |
if (!(params instanceof CoordinatorLayout.LayoutParams)) { | |
throw new IllegalArgumentException("The view is not a child of CoordinatorLayout"); | |
} | |
CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params).getBehavior(); | |
if (!(behavior instanceof ViewPagerBottomSheetBehavior)) { | |
throw new IllegalArgumentException( | |
"The view is not associated with ViewPagerBottomSheetBehavior"); | |
} | |
return (ViewPagerBottomSheetBehavior<V>) behavior; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from medium post, nice work!👍