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

Remove final keyword from mGestureDetector #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -116,6 +116,8 @@ class BouncyAdapter extends RecyclerView.Adapter implements SpringScroller.Sprin
*/
private boolean isSpringFirstValue = true;

private GestureDetectorCompat mGestureDetector;

public BouncyAdapter(Context context, RecyclerView recyclerView,
RecyclerView.Adapter adapter, BouncyConfig config) {
if (recyclerView == null)
Expand All @@ -142,6 +144,7 @@ public BouncyAdapter(Context context, RecyclerView recyclerView,
mSpringScroller = new SpringScroller(config.tension, config.friction, this);

initRecyclerView();
initGestureDectorCompat();
}

@Override
Expand Down Expand Up @@ -349,6 +352,87 @@ public void onTouchEvent(RecyclerView rv, MotionEvent e) {
});
}

private void initGestureDectorCompat() {
mGestureDetector = new GestureDetectorCompat(mContext,
new GestureDetector.SimpleOnGestureListener() {
int scrollByCount = 0;

@Override
public boolean onDown(MotionEvent e) {
scrollByCount = 0;
mFlingOverScrollBack = false;
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
final int headerVisible = getHeaderVisibleLength();
final int footerVisible = getFooterVisibleLength();

int visible = (headerVisible > 0)? headerVisible : footerVisible;

if (visible > 0) {
scrollByCount++;
mFirstScrollBy = (scrollByCount == 1);

double ratioVisible = (double) visible / mGapLimitPx;
float distance = directionVertical()? distanceY : distanceX;
double scrollDist = Math.abs(distance - distance * ratioVisible);

if (distance < 0) {
scrollDist *= -1;
}

scrollBy((int) scrollDist);
}

// still in onTouchEvent, manually scroll the recycler view.
else if (visible == 0 && !mGestureOnIntercept) {
mRecyclerView.scrollBy((int) distanceX, (int) distanceY);
}

return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, final float velocityX, final float velocityY) {
final int headerVisible = getHeaderVisibleLength();
final int footerVisible = getFooterVisibleLength();

float deltaVel = directionVertical()? velocityY : velocityX;
if (mLayoutManager.getReverseLayout())
deltaVel *= -1.0;

final boolean gapVisible = headerVisible > 0 || footerVisible > 0;
final boolean isFlingOverBack = gapVisible && !mGestureOnIntercept &&
((headerVisible > 0 && deltaVel < 0) || (footerVisible > 0 && deltaVel > 0));

// gaps are not visible, use regular fling.
if (!gapVisible && !mGestureOnIntercept) {
mHandlerUI.post(new Runnable() {
@Override
public void run() {
mRecyclerView.fling((int) -velocityX, (int) -velocityY);
}
});
}

// gap is visible, only fling if it's fling back.
else if (isFlingOverBack) {
mHandlerUI.post(new Runnable() {
@Override
public void run() {
mFlingOverScrollBack = true;
mRecyclerView.fling((int) -velocityX, (int) -velocityY);
}
});
}

return true;
}
});
}

private void onActionUp() {
final int footerVisible = getFooterVisibleLength();
final int headerVisible = getHeaderVisibleLength();
Expand Down Expand Up @@ -496,85 +580,6 @@ private int getDecelTargetPos(int headerVisible) {
return getItemCount() - 1;
}

private final GestureDetectorCompat mGestureDetector = new GestureDetectorCompat(mContext,
new GestureDetector.SimpleOnGestureListener() {
int scrollByCount = 0;

@Override
public boolean onDown(MotionEvent e) {
scrollByCount = 0;
mFlingOverScrollBack = false;
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
final int headerVisible = getHeaderVisibleLength();
final int footerVisible = getFooterVisibleLength();

int visible = (headerVisible > 0)? headerVisible : footerVisible;

if (visible > 0) {
scrollByCount++;
mFirstScrollBy = (scrollByCount == 1);

double ratioVisible = (double) visible / mGapLimitPx;
float distance = directionVertical()? distanceY : distanceX;
double scrollDist = Math.abs(distance - distance * ratioVisible);

if (distance < 0) {
scrollDist *= -1;
}

scrollBy((int) scrollDist);
}

// still in onTouchEvent, manually scroll the recycler view.
else if (visible == 0 && !mGestureOnIntercept) {
mRecyclerView.scrollBy((int) distanceX, (int) distanceY);
}

return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, final float velocityX, final float velocityY) {
final int headerVisible = getHeaderVisibleLength();
final int footerVisible = getFooterVisibleLength();

float deltaVel = directionVertical()? velocityY : velocityX;
if (mLayoutManager.getReverseLayout())
deltaVel *= -1.0;

final boolean gapVisible = headerVisible > 0 || footerVisible > 0;
final boolean isFlingOverBack = gapVisible && !mGestureOnIntercept &&
((headerVisible > 0 && deltaVel < 0) || (footerVisible > 0 && deltaVel > 0));

// gaps are not visible, use regular fling.
if (!gapVisible && !mGestureOnIntercept) {
mHandlerUI.post(new Runnable() {
@Override
public void run() {
mRecyclerView.fling((int) -velocityX, (int) -velocityY);
}
});
}

// gap is visible, only fling if it's fling back.
else if (isFlingOverBack) {
mHandlerUI.post(new Runnable() {
@Override
public void run() {
mFlingOverScrollBack = true;
mRecyclerView.fling((int) -velocityX, (int) -velocityY);
}
});
}

return true;
}
});

/**
* Scroll by vertically or horizontally depends on the direction of the
* LinearLayoutManager.
Expand Down