下面列出了androidx.recyclerview.widget.RecyclerView#computeVerticalScrollOffset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
int result = 1;
if (recyclerView instanceof SuperRecyclerView) {
SuperRecyclerView superRecyclerView = (SuperRecyclerView) recyclerView;
result += superRecyclerView.getHeaderContainer().getChildCount();
}
int visibleItemCount = layoutManager.getChildCount();
boolean flag = recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange();
boolean triggerCondition = visibleItemCount > result
&& newState == RecyclerView.SCROLL_STATE_IDLE
&& canTriggerLoadMore(recyclerView) && flag;
if (triggerCondition) {
onLoadMore(recyclerView);
}
}
@Override
public void onScrolled(RecyclerView rv, int dx, int dy) {
final int scrollRange = rv.computeVerticalScrollRange();
final int scrollExtent = rv.computeVerticalScrollExtent();
scrollOffsetRange = scrollRange - scrollExtent;
handleOffsetRange =
scrollExtent - dpToPixel(rv.getContext(), 2 * HANDLE_VERTICAL_MARGIN + HANDLE_SIZE_DP);
scrollOffset = rv.computeVerticalScrollOffset();
if (userControlling) {
return;
}
final float scale = ((float) scrollOffset) / scrollOffsetRange;
handleOffset = (int) (handleOffsetRange * scale);
handleOffsetDV.set((float) handleOffset);
}
public TitleBarViewHelper setRecyclerView(RecyclerView recyclerView) {
mRecyclerView = recyclerView;
if (mRecyclerView == null) {
return this;
}
if (mScrollListener == null) {
mScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//滚动到顶部了
if (recyclerView.computeVerticalScrollOffset() == 0) {
LoggerManager.i("mScrollY:" + mScrollY + ";mAlpha:" + mAlpha);
mScrollY = 0;
} else {
mScrollY += dy;
}
mAlpha = setChange(mScrollY);
}
};
}
mRecyclerView.addOnScrollListener(mScrollListener);
return this;
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
int verticalRange = recyclerView.computeVerticalScrollRange();
//Makes sure the FastScroll is correctly hidden on shorter lists
if (hideOnShortLists && (verticalRange < calculatedMinDisplayHeight || calculatedMinDisplayHeight == 0)) {
updateHandleVisibility(false);
return;
}
//Makes sure the handle is shown when scrolling
updateHandleVisibility(true);
delayHandler.removeCallbacks(handleHideRunnable);
if (handle.isSelected()) {
return;
}
hideHandleDelayed();
float ratio = (float)recyclerView.computeVerticalScrollOffset() / (float) (verticalRange - recyclerView.computeVerticalScrollExtent());
float halfHandleHeight = (handle.getHeight() / 2);
setBubbleAndHandlePosition((height - handle.getHeight()) * ratio + halfHandleHeight);
}
@Override
public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
if (handle.isSelected()) return;
final int offset = recyclerView.computeVerticalScrollOffset();
final int range = recyclerView.computeVerticalScrollRange();
final int extent = recyclerView.computeVerticalScrollExtent();
final int offsetRange = Math.max(range - extent, 1);
setBubbleAndHandlePosition((float) Util.clamp(offset, 0, offsetRange) / offsetRange);
}
private float getScrollProportion(RecyclerView recyclerView) {
if (recyclerView == null) {
return 0;
}
final int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
final int verticalScrollRange = recyclerView.computeVerticalScrollRange();
final float rangeDiff = verticalScrollRange - mViewHeight;
float proportion = (float) verticalScrollOffset / (rangeDiff > 0 ? rangeDiff : 1f);
return mViewHeight * proportion;
}
public void updateContainerAndScrollBarPosition(RecyclerView recyclerView) {
if (!scrollBar.isSelected()) {
int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
int verticalScrollRange = recyclerView.computeVerticalScrollRange();
float proportion = (float) verticalScrollOffset / ((float) verticalScrollRange - getHeight());
setContainerAndScrollBarPosition(getHeight() * proportion);
}
}
private float getScrollProportion(RecyclerView recyclerView) {
if (recyclerView == null) {
return 0;
}
final int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
final int verticalScrollRange = recyclerView.computeVerticalScrollRange();
final float rangeDiff = verticalScrollRange - mViewHeight;
float proportion = (float) verticalScrollOffset / (rangeDiff > 0 ? rangeDiff : 1f);
return mViewHeight * proportion;
}
private float getScrollProportion(RecyclerView recyclerView) {
final int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
final int verticalScrollRange = recyclerView.computeVerticalScrollRange();
final float rangeDiff = verticalScrollRange - mViewHeight;
float proportion = (float) verticalScrollOffset / (rangeDiff > 0 ? rangeDiff : 1f);
return mViewHeight * proportion;
}
private float getScrollProportion(RecyclerView recyclerView) {
if (recyclerView == null) {
return 0;
}
final int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
final int verticalScrollRange = recyclerView.computeVerticalScrollRange();
final float rangeDiff = verticalScrollRange - viewHeight;
float proportion = (float) verticalScrollOffset / (rangeDiff > 0 ? rangeDiff : 1f);
return viewHeight * proportion;
}
/**
* @deprecated as it is based on {@link RecyclerView#computeVerticalScrollOffset()} which only
* returns estimated scroll position, which may lead to the method hanging forever (or until
* hitting max iterations count) Please, consider using {@link Scroller} instead
*/
@Deprecated
public static void scrollTo(final RecyclerView recyclerView, final int targetScrollY) {
final int MAX_ITERATIONS = 100;
int iterations = 0;
while (targetScrollY != recyclerView.computeVerticalScrollOffset()) {
if (iterations > MAX_ITERATIONS) {
throw new RuntimeException(
"Timed out trying to get to the correct scroll position! target: "
+ targetScrollY
+ ", final: "
+ recyclerView.computeVerticalScrollOffset());
}
InstrumentationRegistry.getInstrumentation()
.runOnMainSync(
new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollBy(
0, targetScrollY - recyclerView.computeVerticalScrollOffset());
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
// Sleep because waitForIdleSync doesn't factor in animations (e.g. the scroll animation) that
// go through Choreographer
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
iterations++;
}
}
@Override
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
if (handle.isSelected()) return;
final int offset = recyclerView.computeVerticalScrollOffset();
final int range = recyclerView.computeVerticalScrollRange();
final int extent = recyclerView.computeVerticalScrollExtent();
final int offsetRange = Math.max(range - extent, 1);
setBubbleAndHandlePosition((float) Util.clamp(offset, 0, offsetRange) / offsetRange);
}
/**
* 计算垂直滚动偏移
*
* @return 滚动偏移
*/
public int computeVerticalScrollOffset() {
if (getOrientation() == RecyclerView.VERTICAL) {
final RecyclerView view = getRecyclerView();
return view == null ? 0 : view.computeVerticalScrollOffset();
}
return computeAnotherDirectionScrollOffset();
}
private static boolean isSlideToBottom(RecyclerView recyclerView) {
return recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset()
>= recyclerView.computeVerticalScrollRange();
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (recyclerView.getChildCount() > 0) {
firstCardMarginTop = recyclerView.getChildAt(0).getMeasuredHeight();
} else {
firstCardMarginTop = -1;
}
scrollY = recyclerView.computeVerticalScrollOffset();
lastAppBarTranslationY = binding.appBar.getTranslationY();
weatherView.onScroll(scrollY);
if (adapter != null) {
adapter.onScroll(recyclerView);
}
// set translation y of toolbar.
if (adapter != null && firstCardMarginTop > 0) {
if (firstCardMarginTop
>= binding.appBar.getMeasuredHeight() + adapter.getCurrentTemperatureTextHeight(recyclerView)) {
if (scrollY < firstCardMarginTop
- binding.appBar.getMeasuredHeight()
- adapter.getCurrentTemperatureTextHeight(recyclerView)) {
binding.appBar.setTranslationY(0);
} else if (scrollY > firstCardMarginTop - binding.appBar.getY()) {
binding.appBar.setTranslationY(-binding.appBar.getMeasuredHeight());
} else {
binding.appBar.setTranslationY(
firstCardMarginTop
- adapter.getCurrentTemperatureTextHeight(recyclerView)
- scrollY
- binding.appBar.getMeasuredHeight()
);
}
} else {
binding.appBar.setTranslationY(-scrollY);
}
}
// set system bar style.
if (firstCardMarginTop <= 0) {
topChanged = true;
topOverlap = false;
} else {
topChanged = (binding.appBar.getTranslationY() != 0) != (lastAppBarTranslationY != 0);
topOverlap = binding.appBar.getTranslationY() != 0;
}
if (topChanged) {
weatherView.setSystemBarColor(MainActivity.this, getWindow(),
topOverlap, false, true, false);
}
}
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!Preferences.isViewerSwipeToTurn() || !isScrollEnabled) {
recyclerView.stopScroll();
return;
}
LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
if (llm != null) {
if (RecyclerView.SCROLL_STATE_DRAGGING == newState) {
dragStartPositionX = recyclerView.computeHorizontalScrollOffset();
dragStartPositionY = recyclerView.computeVerticalScrollOffset();
isSettlingX = false;
isSettlingY = false;
} else if (RecyclerView.SCROLL_STATE_SETTLING == newState) {
// If the settling position is different from the original position, ignore that scroll
// (e.g. snapping back to the original position after a small scroll)
if (recyclerView.computeHorizontalScrollOffset() != dragStartPositionX)
isSettlingX = true;
if (recyclerView.computeVerticalScrollOffset() != dragStartPositionY)
isSettlingY = true;
} else if (RecyclerView.SCROLL_STATE_IDLE == newState) {
// Don't do anything if we're not on a boundary
if (!(llm.findLastVisibleItemPosition() == llm.getItemCount() - 1 || 0 == llm.findFirstVisibleItemPosition()))
return;
if (recyclerView.computeHorizontalScrollOffset() == dragStartPositionX && !isSettlingX && llm.canScrollHorizontally()) {
if (0 == dragStartPositionX && !llm.getReverseLayout())
onStartOutOfBoundScroll.run();
else if (0 == dragStartPositionX) onEndOutOfBoundScroll.run();
else if (llm.getReverseLayout()) onStartOutOfBoundScroll.run();
else onEndOutOfBoundScroll.run();
}
if (recyclerView.computeVerticalScrollOffset() == dragStartPositionY && !isSettlingY && llm.canScrollVertically()) {
if (0 == dragStartPositionY && !llm.getReverseLayout())
onStartOutOfBoundScroll.run();
else if (0 == dragStartPositionY) onEndOutOfBoundScroll.run();
else if (llm.getReverseLayout()) onStartOutOfBoundScroll.run();
else onEndOutOfBoundScroll.run();
}
}
}
}