-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modify behavior and provide convenience tools
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
vpbs/src/main/java/android/support/v4/view/ViewPagerUtils.java
This file contains 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
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
51
vpbs/src/main/java/biz/laenger/android/vpbs/BottomSheetUtils.java
This file contains 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
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; | ||
} | ||
|
||
} |
This file contains 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
2775715
There was a problem hiding this comment.
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)
2775715
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KethuY hi, you can use this -- https://stackoverflow.com/a/56325869/8224757
2775715
There was a problem hiding this comment.
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.
2775715
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2775715
There was a problem hiding this comment.
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 😅