Java Code Examples for androidx.recyclerview.widget.RecyclerView#computeVerticalScrollOffset()

The following examples show how to use androidx.recyclerview.widget.RecyclerView#computeVerticalScrollOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OnLoadMoreScrollListener.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source File: ScrollController.java    From litho with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: TitleBarViewHelper.java    From FastLib with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: FastScroll.java    From RecyclerExt with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 5
Source File: RecyclerViewFastScroller.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: FastScroller.java    From a with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: FastScroller.java    From call_manage with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 8
Source File: FastScroller.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: Pix.java    From PixImagePicker with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: FastScroller.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: InteractionUtil.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * @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++;
  }
}
 
Example 12
Source File: RecyclerViewFastScroller.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 13
Source File: BothDirectionsScrollLayoutManager.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
/**
 * 计算垂直滚动偏移
 *
 * @return 滚动偏移
 */
public int computeVerticalScrollOffset() {
    if (getOrientation() == RecyclerView.VERTICAL) {
        final RecyclerView view = getRecyclerView();
        return view == null ? 0 : view.computeVerticalScrollOffset();
    }
    return computeAnotherDirectionScrollOffset();
}
 
Example 14
Source File: InfiniteScrollListener.java    From HaoReader with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isSlideToBottom(RecyclerView recyclerView) {
    return recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset()
            >= recyclerView.computeVerticalScrollRange();
}
 
Example 15
Source File: MainActivity.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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);
    }
}
 
Example 16
Source File: ScrollPositionListener.java    From Hentoid with Apache License 2.0 4 votes vote down vote up
@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();
            }
        }
    }
}