Skip to content

Commit

Permalink
Fixed fast items' scroll, when we don't get an idle event during this…
Browse files Browse the repository at this point in the history
… scroll between the pages and get it only eventually for the last page
  • Loading branch information
devnullorthrow committed Oct 28, 2019
1 parent 51e3f67 commit 2d389be
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.yoksnod:ParallaxBackgroundViewPager:v0.6'
implementation project(':library')
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected void onCreate(Bundle savedInstanceState) {
FragmentPagerAdapter adapter = new ParallaxFragmentPagerAdapter(getSupportFragmentManager(), items);
pager.setAdapter(adapter);
pager.addOnPageChangeListener(new ParallaxBackgroundPageListener(this, pager, adapter, items));
pager.startAutoScroll();
// pager.startAutoScroll();
}

private class ParallaxFragmentPagerAdapter extends FragmentPagerAdapter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.yoksnod.parallaxbackgroundviewpager;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.util.List;
import java.util.NoSuchElementException;

public class ParallaxBackgroundPageListener implements ViewPager.OnPageChangeListener {

private static final String TAG = "ParallaxListener";
private static final String ANDROID_SWITCHER_TAG_SEGMENT = "android:switcher:";
private static final String SEPARATOR_TAG_SEGMENT = ":";
private static final float POSITION_OFFSET_BASE = 0.5f;
Expand Down Expand Up @@ -48,27 +50,27 @@ private static String makePagerFragmentTag(int index, int pagerId) {

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.d(TAG, "onPageScrolled position = " + position + " currentPagePosition = " + currentPagePosition + " shouldCalculateScrollDirection = " + shouldCalculateScrollDirection);
recalculateScrollDirection(positionOffset, positionOffsetPixels);

int scrollX = pager.getScrollX();
if (canScrollToLeft(scrollX) || isRightEdge(scrollX)) {
return;
}
int animatedItemIndex = isScrollToRight
? Math.min(currentPagePosition, adapter.getCount() - 1)
: Math.max(0, currentPagePosition - 1);
Log.d(TAG, "onPageScrolled isRight = " + isScrollToRight + " anim index = " + animatedItemIndex);

int animatedItemIndex = isScrollToRight ? currentPagePosition : currentPagePosition - 1;
setAlpha(animatedItemIndex, scrollX);


if (isLeftEdge(scrollX)) {
restoreInitialAlphaValues();
}
}

private boolean canScrollToLeft(int scrollX) {
return isLeftEdge(scrollX) && !isScrollToRight;
}

private void setAlpha(int animatedItemIndex, int scrollX) {
View child = findFragmentViewByIndex(animatedItemIndex);
if (child == null)
return;
ViewPager.LayoutParams lp = (ViewPager.LayoutParams) child.getLayoutParams();
if (lp.isDecor) {
return;
Expand All @@ -77,11 +79,12 @@ private void setAlpha(int animatedItemIndex, int scrollX) {
initCurrentAlpha(transformPos, animatedItemIndex);
}

@Nullable
private View findFragmentViewByIndex(int index) {
String tag = makePagerFragmentTag(index, pager.getId());
Fragment page = activity.getSupportFragmentManager().findFragmentByTag(tag);
if (page == null)
throw new NoSuchElementException("no such element for tag : " + tag);
if (page == null || page.getView() == null)
return null;

return page.getView();
}
Expand All @@ -101,10 +104,6 @@ private void initCurrentAlpha(float transformPos, int itemIndex) {
}
}

private boolean isRightEdge(int scrollX) {
return scrollX == pager.getWidth() * adapter.getCount();
}


private void restoreInitialAlphaValues() {
for (int j = items.size() - 1; j >= 0; j--) {
Expand All @@ -130,24 +129,41 @@ public void onPageSelected(int position) {
if (position == 0) {
onPageScrollStateChanged(ViewPager.SCROLL_STATE_IDLE);
}

Log.d(TAG, "onPageSelected position = " + position + " currentPagePosition = " + currentPagePosition + " pager current item = " + pager.getCurrentItem());

if (Math.abs(currentPagePosition - position) > 1) {
currentPagePosition = isScrollToRight
? Math.max(0, position - 1)
: Math.min(position + 1, adapter.getCount() - 1);
}

}

@Override
public void onPageScrollStateChanged(int state) {
currentPageScrollState = state;
Log.d(TAG, "onPageScrollStateChanged state = " + PagerState.values()[state] + " currentPagePosition = " + currentPagePosition + " pager current item = " + pager.getCurrentItem());


if (state == ViewPager.SCROLL_STATE_IDLE) {
currentPagePosition = pager.getCurrentItem();
isScrollToRight = true;
}

boolean isDragScroll = isDragScroll();
isScrollStarted = isDragScroll;
if (isDragScroll) {
boolean isIdle = isIdleScroll();
isScrollStarted = isIdle;
if (isIdle) {
shouldCalculateScrollDirection = true;
}
}

private boolean isDragScroll() {
return !isScrollStarted && currentPageScrollState == ViewPager.SCROLL_STATE_DRAGGING;
private boolean isIdleScroll() {
return !isScrollStarted && currentPageScrollState == ViewPager.SCROLL_STATE_IDLE;
}

private enum PagerState {
SCROLL_STATE_IDLE,
SCROLL_STATE_DRAGGING,
SCROLL_STATE_SETTLING
}
}

0 comments on commit 2d389be

Please sign in to comment.