ViewPager migrate to ViewPager2. Fixes #522.

Swiping between tabs work. Fixes #105.
This commit is contained in:
Román Martínez
2020-11-26 22:07:58 +01:00
parent e8017a4e9d
commit ae8833d21b
6 changed files with 87 additions and 31 deletions
@@ -30,6 +30,7 @@ import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewParent;
import android.widget.Scroller;
import androidx.annotation.NonNull;
@@ -117,6 +118,14 @@ public class ChartView extends View {
private boolean reportSpeed = true;
private boolean showPointer = false;
// To handle disallow intercept touch event in parent.
private static final int SCROLL_X_NONE = 0;
private static final int SCROLL_X_LEFT = 1;
private static final int SCROLL_X_RIGHT = 2;
private boolean isOnLeftEdge = true;
private boolean isOnRightEdge = true;
private int scrollXDirection = SCROLL_X_NONE;
private final GestureDetectorCompat detectorScrollFlingTab = new GestureDetectorCompat(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
@@ -129,6 +138,14 @@ public class ChartView extends View {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (distanceX < 0) {
scrollXDirection = SCROLL_X_LEFT;
} else if (distanceX > 0) {
scrollXDirection = SCROLL_X_RIGHT;
} else {
scrollXDirection = SCROLL_X_NONE;
}
if (Math.abs(distanceX) > 0) {
int availableToScroll = effectiveWidth * (zoomLevel - 1) - getScrollX();
if (availableToScroll > 0) {
@@ -434,6 +451,9 @@ public class ChartView extends View {
chartPoints.clear();
xExtremityMonitor.reset();
zoomLevel = 1;
isOnLeftEdge = true;
isOnRightEdge = true;
scrollXDirection = SCROLL_X_NONE;
updateDimensions();
}
}
@@ -500,6 +520,18 @@ public class ChartView extends View {
invalidate();
}
/**
* Handle parent's view disallow touch event.
*
* @param disallow Does disallow parent touch event?
*/
private void requestDisallowInterceptTouchEventInParent(boolean disallow) {
ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(disallow);
}
}
/**
* Scrolls the view horizontally by a given amount.
*
@@ -507,13 +539,21 @@ public class ChartView extends View {
*/
private void scrollBy(int deltaX) {
int scrollX = getScrollX() + deltaX;
if (scrollX < 0) {
if (scrollX <= 0) {
scrollX = 0;
isOnLeftEdge = true;
} else {
isOnLeftEdge = false;
}
int maxWidth = effectiveWidth * (zoomLevel - 1);
if (scrollX > maxWidth) {
if (scrollX >= maxWidth) {
scrollX = maxWidth;
isOnRightEdge = true;
} else {
isOnRightEdge = false;
}
scrollTo(scrollX, 0);
}
@@ -538,6 +578,15 @@ public class ChartView extends View {
public boolean onTouchEvent(MotionEvent event) {
boolean isZoom = detectorZoom.onTouchEvent(event);
boolean isScrollTab = detectorScrollFlingTab.onTouchEvent(event);
if (event.getPointerCount() == 1 && ((isOnLeftEdge && scrollXDirection == SCROLL_X_LEFT) || (isOnRightEdge && scrollXDirection == SCROLL_X_RIGHT))) {
// Focus on view's parent.
requestDisallowInterceptTouchEventInParent(false);
} else {
// Focus on ChartView.
requestDisallowInterceptTouchEventInParent(true);
}
return isZoom || isScrollTab;
}