Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for nested scrolling in a CoordinatorLayout #827

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.graphics.PointF;
import android.graphics.RectF;
import android.support.v4.view.ViewCompat;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
Expand Down Expand Up @@ -160,6 +161,7 @@ public boolean onDoubleTapEvent(MotionEvent e) {

@Override
public boolean onDown(MotionEvent e) {
pdfView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
animationManager.stopFling();
return true;
}
Expand All @@ -177,6 +179,10 @@ public boolean onSingleTapUp(MotionEvent e) {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
scrolling = true;

pdfView.dispatchNestedPreScroll(0, (int) distanceY, null, null);
pdfView.dispatchNestedScroll(0, 0, 0, 0, null);

if (pdfView.isZooming() || pdfView.isSwipeEnabled()) {
pdfView.moveRelativeTo(-distanceX, -distanceY);
}
Expand All @@ -187,6 +193,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
}

private void onScrollEnd(MotionEvent event) {
pdfView.stopNestedScroll();
pdfView.loadPages();
hideHandle();
if (!animationManager.isFlinging()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@
import android.os.AsyncTask;
import android.os.Build;
import android.os.HandlerThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.github.barteksc.pdfviewer.exception.PageRenderingException;
Expand Down Expand Up @@ -91,7 +96,7 @@
* using {@link #load(DocumentSource, String, int[])}. In this
* particular case, a userPage of 5 can refer to a documentPage of 17.
*/
public class PDFView extends RelativeLayout {
public class PDFView extends RelativeLayout implements NestedScrollingChild {

private static final String TAG = PDFView.class.getSimpleName();

Expand Down Expand Up @@ -123,6 +128,9 @@ enum ScrollDir {
/** Drag manager manage all touch events */
private DragPinchManager dragPinchManager;

/** Helper for NestedScrolling in CoordinatorLayout */
private NestedScrollingChildHelper scrollingChildHelper;

PdfFile pdfFile;

/** The index of the current sequence */
Expand Down Expand Up @@ -254,6 +262,7 @@ public PDFView(Context context, AttributeSet set) {
cacheManager = new CacheManager();
animationManager = new AnimationManager(this);
dragPinchManager = new DragPinchManager(this, animationManager);
scrollingChildHelper = new NestedScrollingChildHelper(this);
pagesLoader = new PagesLoader(this);

paint = new Paint();
Expand Down Expand Up @@ -473,9 +482,16 @@ protected void onDetachedFromWindow() {
}
renderingHandlerThread = null;
}

scrollingChildHelper.onDetachedFromWindow();
super.onDetachedFromWindow();
}

@Override
public void onStopNestedScroll(@NonNull View child) {
scrollingChildHelper.onStopNestedScroll(child);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
hasSize = true;
Expand Down Expand Up @@ -1317,6 +1333,51 @@ public Configurator fromSource(DocumentSource docSource) {
return new Configurator(docSource);
}

@Override
public void setNestedScrollingEnabled(boolean enabled) {
scrollingChildHelper.setNestedScrollingEnabled(enabled);
}

@Override
public boolean isNestedScrollingEnabled() {
return scrollingChildHelper.isNestedScrollingEnabled();
}

@Override
public boolean hasNestedScrollingParent() {
return scrollingChildHelper.hasNestedScrollingParent();
}

@Override
public boolean startNestedScroll(int axes) {
return scrollingChildHelper.startNestedScroll(axes);
}

@Override
public void stopNestedScroll() {
scrollingChildHelper.stopNestedScroll();
}

@Override
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow) {
return scrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
}

@Override
public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow) {
return scrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
}

@Override
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
return scrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
}

@Override
public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
return scrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
}

private enum State {DEFAULT, LOADED, SHOWN, ERROR}

public class Configurator {
Expand Down