Skip to content

Commit

Permalink
modify behavior and provide convenience tools
Browse files Browse the repository at this point in the history
  • Loading branch information
laenger committed Jul 8, 2016
1 parent cfdce8b commit 2775715
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
19 changes: 19 additions & 0 deletions vpbs/src/main/java/android/support/v4/view/ViewPagerUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package android.support.v4.view;

import android.view.View;

public class ViewPagerUtils {

public static View getCurrentView(ViewPager viewPager) {
final int currentItem = viewPager.getCurrentItem();
for (int i = 0; i < viewPager.getChildCount(); i++) {
final View child = viewPager.getChildAt(i);
final ViewPager.LayoutParams layoutParams = (ViewPager.LayoutParams) child.getLayoutParams();
if (!layoutParams.isDecor && currentItem == layoutParams.position) {
return child;
}
}
return null;
}

}
51 changes: 51 additions & 0 deletions vpbs/src/main/java/biz/laenger/android/vpbs/BottomSheetUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package biz.laenger.android.vpbs;

import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

public final class BottomSheetUtils {

public static void setupViewPager(ViewPager viewPager) {
final View bottomSheetParent = findBottomSheetParent(viewPager);
if (bottomSheetParent != null) {
viewPager.addOnPageChangeListener(new BottomSheetViewPagerListener(viewPager, bottomSheetParent));
}
}

private static class BottomSheetViewPagerListener extends ViewPager.SimpleOnPageChangeListener {
private final ViewPager viewPager;
private final ViewPagerBottomSheetBehavior<View> behavior;

public BottomSheetViewPagerListener(ViewPager viewPager, View bottomSheetParent) {
this.viewPager = viewPager;
this.behavior = ViewPagerBottomSheetBehavior.from(bottomSheetParent);
}

@Override
public void onPageSelected(int position) {
viewPager.post(new Runnable() {
@Override
public void run() {
behavior.invalidateScrollingChild();
}
});
}
}

private static View findBottomSheetParent(final View view) {
View current = view;
while (current != null) {
final ViewGroup.LayoutParams params = current.getLayoutParams();
if (params instanceof CoordinatorLayout.LayoutParams && ((CoordinatorLayout.LayoutParams) params).getBehavior() instanceof ViewPagerBottomSheetBehavior) {
return current;
}
final ViewParent parent = current.getParent();
current = parent == null || !(parent instanceof View) ? null : (View) parent;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.VelocityTrackerCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPagerUtils;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
Expand Down Expand Up @@ -391,6 +393,11 @@ public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, Vi
velocityX, velocityY));
}

void invalidateScrollingChild() {
final View scrollingChild = findScrollingChild(mViewRef.get());
mNestedScrollingChildRef = new WeakReference<>(scrollingChild);
}

/**
* Sets the height of the bottom sheet when it is collapsed.
*
Expand Down Expand Up @@ -548,7 +555,14 @@ private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
return view;
}
if (view instanceof ViewGroup) {
if (view instanceof ViewPager) {
ViewPager viewPager = (ViewPager) view;
View currentViewPagerChild = ViewPagerUtils.getCurrentView(viewPager);
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));
Expand Down

5 comments on commit 2775715

@KethuY
Copy link

@KethuY KethuY commented on 2775715 May 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In viewpagerutils, position is can't be resoved
how to fix this
if (!layoutParams.isDecor && currentItem == layoutParams.position)

@terofeev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Devansh-Maurya
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KethuY Check the package name of ViewPagerUtils. Give it the same package as the package which contains ViewPager.LayoutParams and it'll work (in Kotlin). For Java, also follow the same directory structure as the package name.

@KethuY
Copy link

@KethuY KethuY commented on 2775715 Oct 14, 2020 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Devansh-Maurya
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome! I just left this comment here so that later if I come across the same issue again, I can save some time 😅

Please sign in to comment.