Last active
February 3, 2017 18:38
-
-
Save brendanw/f71557a77d2a86d87a5286bd0465e8b4 to your computer and use it in GitHub Desktop.
RecyclerView Scroll Tracking using a Sentinel View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
headerTrackScrollListener = new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
float sentinelYPos = adapter.getSentinelViewYPos(); | |
if (sentinelYPos >= Utility.getScreenHeight(getContext()) || !areAuxiliaryTouchEventsEnabled) { | |
return; | |
} | |
float topOfListScrollY = sentinelYPos - headerHeight; | |
listBackgroundView.setY(topOfListScrollY); | |
cameraPreviewView.setY(sentinelYPos - cameraPreviewHeight - headerHeight); | |
if (topOfListScrollY <= 0 && headerView.getY() != 0 && prevTopOfListScrollY >= 0) { | |
//WE ARE TRANSITIONING FROM PREVIEW BEING VISIBLE TO IT NOT BEING VISIBLE | |
isCameraPreviewVisible = false; | |
headerView.translateY(0 - (int) headerView.getY()); | |
cameraPresenter.onPause(); | |
cameraView.setVisibility(View.GONE); | |
} else if (topOfListScrollY >= 0 && prevTopOfListScrollY <= 0) { | |
//WE ARE TRANSITIONING FROM PREVIEW NOT BEING VISIBLE TO BEING VISIBLE | |
isCameraPreviewVisible = true; | |
headerView.cancelAnimators(); | |
headerView.setY(topOfListScrollY); | |
if (!shouldTurnCameraOffUntilFullyVisible) { | |
cameraView.setVisibility(View.VISIBLE); | |
} | |
} else if (topOfListScrollY > 0) { | |
//PREVIEW IS VISIBLE AND HAS BEEN VISIBLE | |
headerView.setY(topOfListScrollY); | |
if (isVisible && !shouldTurnCameraOffUntilFullyVisible) { | |
cameraPresenter.onResume(getContext()); | |
cameraView.setVisibility(View.VISIBLE); | |
} | |
} | |
prevTopOfListScrollY = topOfListScrollY; | |
} | |
}; | |
recyclerView.addOnScrollListener(headerTrackScrollListener); | |
----------- | |
cameraPreviewView.setOnTouchListener((v, event) -> { | |
if (isCameraHolderAnimating) { | |
return false; | |
} | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: { | |
((NonScrollableGridLayoutManager) recyclerView.getLayoutManager()).setScrollEnabled(false); | |
headerView.setAreTouchEventsEnabled(false); | |
((LithiumActivity) getContext()).getTabHolder().setAreTouchEventsEnabled(false); | |
if (velocityTracker == null) { | |
velocityTracker = VelocityTracker.obtain(); | |
} else { | |
velocityTracker.clear(); | |
} | |
prevY = event.getRawY(); | |
velocityTracker.addMovement(event); | |
break; | |
} | |
case MotionEvent.ACTION_MOVE: { | |
velocityTracker.addMovement(event); | |
float dy = event.getRawY() - prevY; | |
prevY = event.getRawY(); | |
totalDy += Math.abs(dy); | |
if (isDragging || Math.abs(dy) > touchSlop) { | |
isDragging = true; | |
float newY = studioHolder.getY() + dy; | |
if (newY < 0) { | |
newY = 0; | |
} | |
studioHolder.setY(newY); | |
updateCameraPreview(0f); | |
} | |
break; | |
} | |
case MotionEvent.ACTION_CANCEL: | |
case MotionEvent.ACTION_UP: { | |
isDragging = false; | |
float yVel = 1; | |
if (totalDy > ViewUtils.dpToPx(3, getContext())) { | |
velocityTracker.computeCurrentVelocity(1000); | |
yVel = velocityTracker.getYVelocity(); | |
} | |
if (yVel > 0) { | |
showCamera(); | |
} else { | |
animateToDefault(); | |
} | |
velocityTracker.recycle(); | |
velocityTracker = null; | |
totalDy = 0; | |
break; | |
} | |
} | |
return true; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment