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

OverScrollView for android #1365

Draft
wants to merge 21 commits into
base: development
Choose a base branch
from
Draft
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
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new ReactOverScrollPackage());

return packages;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tonlabs.uikit;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;

public class ReactOverScrollPackage implements ReactPackage {
@Nonnull
@Override
public List<NativeModule> createNativeModules(@Nonnull ReactApplicationContext reactContext) {
return Collections.emptyList();
}

@Nonnull
@Override
public List<ViewManager> createViewManagers(@Nonnull ReactApplicationContext reactContext) {
return Collections.emptyList();
// List<ViewManager> modules = new ArrayList<>();
// modules.add(new ReactOverScrollViewManager());
// return modules;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
package tonlabs.uikit;

import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.events.NativeGestureUtil;
import com.facebook.react.views.scroll.ReactScrollView;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.core.view.MotionEventCompat;

import com.facebook.react.views.scroll.ReactScrollViewHelper;
import com.facebook.react.views.scroll.ScrollEvent;
import com.facebook.react.views.scroll.ScrollEventType;
import com.mixiaoxiao.overscroll.OverScrollDelegate;
import com.mixiaoxiao.overscroll.OverScrollDelegate.OverScrollable;
import com.mixiaoxiao.overscroll.PathScroller;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

/**
* https://github.com/Mixiaoxiao/OverScroll-Everywhere
*
* @author Mixiaoxiao 2016-08-31
*/
public class ReactOverScrollView extends ReactScrollView implements OverScrollable {

private OverScrollDelegate mOverScrollDelegate;

// ===========================================================
// Constructors
// ===========================================================
public ReactOverScrollView(ReactContext context) {
super(context);
createOverScrollDelegate(context);
}

// ===========================================================
// createOverScrollDelegate
// ===========================================================
private void createOverScrollDelegate(Context context) {
mOverScrollDelegate = new OverScrollDelegate(this);
mOverScrollDelegate.setOverScrollStyle(new OverScrollStyle());
mOverScrollDelegate.setOverScrollType(true, false);
}

// ===========================================================
// Delegate
// ===========================================================
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!getScrollEnabled()) {
return false;
}

final int action = MotionEventCompat.getActionMasked(ev);
if (action == MotionEvent.ACTION_DOWN) {
setParentDragging(true);
}

boolean parentIntercepted = super.onInterceptTouchEvent(ev);
boolean overScrollIntercepted = mOverScrollDelegate.onInterceptTouchEvent(ev);

return parentIntercepted || overScrollIntercepted;
}

Field parentDraggingField;

boolean getParentDragging() {
try {
// TODO: create a wrapper with getter method
if (parentDraggingField == null) {
parentDraggingField = getClass().getSuperclass().getDeclaredField("mDragging"); //NoSuchFieldException
parentDraggingField.setAccessible(true);
}
return (boolean) parentDraggingField.get(this);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return false;
}

void setParentDragging(boolean val) {
try {
// TODO: create a wrapper with getter method
if (parentDraggingField == null) {
parentDraggingField = getClass().getSuperclass().getDeclaredField("mDragging"); //NoSuchFieldException
parentDraggingField.setAccessible(true);
}
parentDraggingField.set(this, val);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}

Field mScrollEnabledField;

boolean getScrollEnabled() {
try {
// TODO: create a wrapper with getter method
if (mScrollEnabledField == null) {
mScrollEnabledField = getClass().getSuperclass().getDeclaredField("mScrollEnabled"); //NoSuchFieldException
mScrollEnabledField.setAccessible(true);
}
return (boolean) mScrollEnabledField.get(this);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!getScrollEnabled()) {
return false;
}

if (mOverScrollDelegate.onTouchEvent(ev)) {
int mState = getOverScrollState();
if (mState == OverScrollDelegate.OS_DRAG_TOP || mState == OverScrollDelegate.OS_DRAG_BOTTOM) {
ReactContext reactContext = (ReactContext) this.getContext();
int surfaceId = UIManagerHelper.getSurfaceId(reactContext);
Log.d("ReactOverScrollView", "custom onScroll");
UIManagerHelper
.getEventDispatcherForReactTag(reactContext, this.getId())
.dispatchEvent(
ScrollEvent.obtain(
surfaceId,
this.getId(),
ScrollEventType.SCROLL,
0,
(int) (-1 * getOverScrollOffsetY() * ev.getYPrecision()),
0,
0,
0,
0,
0,
0));
}
return true;
}

return super.onTouchEvent(ev);
}

Field overScrollStateField;

int getOverScrollState() {
try {
// TODO: create a wrapper with getter method
if (overScrollStateField == null) {
overScrollStateField = mOverScrollDelegate.getClass().getDeclaredField("mState"); //NoSuchFieldException
overScrollStateField.setAccessible(true);
}
return (int) overScrollStateField.get(mOverScrollDelegate);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return 0;
}

Field overScrollOffsetYField;

float getOverScrollOffsetY() {
try {
// TODO: create a wrapper with getter method
if (overScrollOffsetYField == null) {
overScrollOffsetYField = mOverScrollDelegate.getClass().getDeclaredField("mOffsetY"); //NoSuchFieldException
overScrollOffsetYField.setAccessible(true);
}
return (float) overScrollOffsetYField.get(mOverScrollDelegate);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return 0.0F;
}

Method superHandlePostTouchScrolling;

public void callSuperHandlePostTouchScrolling(int velocityX, int velocityY) {
try {
if (superHandlePostTouchScrolling == null) {
superHandlePostTouchScrolling = Objects.requireNonNull(getClass().getSuperclass()).getDeclaredMethod("handlePostTouchScrolling", int.class, int.class);
superHandlePostTouchScrolling.setAccessible(true);
}
superHandlePostTouchScrolling.invoke(this, velocityX, velocityY);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}

@Override
public void draw(Canvas canvas) {
mOverScrollDelegate.draw(canvas);
}

@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
return mOverScrollDelegate.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY,
maxOverScrollX, maxOverScrollY, isTouchEvent);
}

// ===========================================================
// OverScrollable, aim to call view internal methods
// ===========================================================

@Override
public int superComputeVerticalScrollExtent() {
return super.computeVerticalScrollExtent();
}

@Override
public int superComputeVerticalScrollOffset() {
return super.computeVerticalScrollOffset();
}

@Override
public int superComputeVerticalScrollRange() {
return super.computeVerticalScrollRange();
}

@Override
public void superOnTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
}

@Override
public void superDraw(Canvas canvas) {
super.draw(canvas);
}

@Override
public boolean superAwakenScrollBars() {
return super.awakenScrollBars();
}

@Override
public boolean superOverScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX,
maxOverScrollY, isTouchEvent);
}

@Override
public View getOverScrollableView() {
return this;
}

@Override
public OverScrollDelegate getOverScrollDelegate() {
return mOverScrollDelegate;
}

@Override
public void scrollTo(int x, int y) {
try {
// TODO: create a wrapper with getter method
Field mStateField = mOverScrollDelegate.getClass().getDeclaredField("mState"); //NoSuchFieldException
mStateField.setAccessible(true);
int mState = (int) mStateField.get(mOverScrollDelegate);

// TODO: create a wrapper with getter method
Field mOffsetYField = mOverScrollDelegate.getClass().getDeclaredField("mOffsetY"); //NoSuchFieldException
mOffsetYField.setAccessible(true);

if (mState == OverScrollDelegate.OS_DRAG_TOP || mState == OverScrollDelegate.OS_DRAG_BOTTOM) {
mStateField.set(mOverScrollDelegate, OverScrollDelegate.OS_NONE);
mOffsetYField.set(mOverScrollDelegate, y);
invalidate();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
super.scrollTo(x, y);
}

public static class OverScrollStyle extends OverScrollDelegate.OverScrollStyle {
public void transformOverScrollCanvas(float offsetY, Canvas canvas, View view) {
canvas.translate(0.0F, offsetY);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tonlabs.uikit;

import androidx.annotation.NonNull;

import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.views.scroll.ReactScrollView;
import com.facebook.react.views.scroll.ReactScrollViewManager;

import org.jetbrains.annotations.NotNull;


@ReactModule(name = ReactOverScrollViewManager.REACT_CLASS)
public class ReactOverScrollViewManager extends ReactScrollViewManager {
public static final String REACT_CLASS = "RCTScrollView";

@NonNull
@NotNull
@Override
public ReactScrollView createViewInstance(ThemedReactContext reactContext) {
return new ReactOverScrollView(reactContext);
}

@Override
public boolean canOverrideExistingModule() {
return true;
}
}
1 change: 1 addition & 0 deletions Example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"react-native-screens": "3.6.0",
"react-native-share": "^3.8.3",
"react-native-simple-popover": "git+https://github.com/tonlabs/react-native-simple-popover.git",
"react-native-simple-shadow-view": "1.6.3",
"react-native-status-bar-height": "1.0.1",
"react-native-svg": "^12.1.0",
"react-native-view-shot": "3.1.2",
Expand Down
Loading