Java Code Examples for android.support.v7.widget.RecyclerView#computeVerticalScrollOffset()

The following examples show how to use android.support.v7.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: Utils.java    From Expert-Android-Programming with MIT License 6 votes vote down vote up
public static boolean syncOffset(SmoothAppBarLayout smoothAppBarLayout, View target, int verticalOffset, View scroll) {
  boolean isSelected = target == scroll;
  if (scroll instanceof NestedScrollView) {
    NestedScrollView nestedScrollView = (NestedScrollView) scroll;
    if (nestedScrollView.getScrollY() < verticalOffset || (!isSelected && isScrollToTop(target))) {
      nestedScrollView.scrollTo(0, verticalOffset);
    }
    if (isSelected && (nestedScrollView.getScrollY() < verticalOffset || verticalOffset == 0)) {
      nestedScrollView.scrollTo(0, 0);
      smoothAppBarLayout.syncOffset(0);
    }
  } else if (scroll instanceof RecyclerView) {
    RecyclerView recyclerView = (RecyclerView) scroll;
    boolean isAccuracy = recyclerView.getLayoutManager().findViewByPosition(ObservableRecyclerView.HEADER_VIEW_POSITION) != null;
    if (isAccuracy && recyclerView.computeVerticalScrollOffset() < verticalOffset) {
      recyclerView.scrollBy(0, verticalOffset - recyclerView.computeVerticalScrollOffset());
    } else if (!isSelected && isScrollToTop(target)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
    }
    if (isAccuracy && isSelected && (recyclerView.computeVerticalScrollOffset() < verticalOffset || verticalOffset == 0)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
      smoothAppBarLayout.syncOffset(0);
    }
  }
  return true;
}
 
Example 2
Source File: Utils.java    From MaterialViewPager with Apache License 2.0 6 votes vote down vote up
static boolean canScroll(View view) {
    if (view instanceof ScrollView) {
        ScrollView scrollView = (ScrollView) view;
        View child = scrollView.getChildAt(0);
        if (child != null) {
            int childHeight = child.getHeight();
            return scrollView.getHeight() < childHeight + scrollView.getPaddingTop() + scrollView.getPaddingBottom();
        }
        return false;
    } else if (view instanceof RecyclerView) {
        RecyclerView recyclerView = (RecyclerView) view;
        int yOffset = recyclerView.computeVerticalScrollOffset();
        return yOffset != 0;
    }
    return true;
}
 
Example 3
Source File: Utils.java    From smooth-app-bar-layout with Apache License 2.0 6 votes vote down vote up
public static boolean syncOffset(SmoothAppBarLayout smoothAppBarLayout, View target, int verticalOffset, View scroll) {
  boolean isSelected = target == scroll;
  if (scroll instanceof NestedScrollView) {
    NestedScrollView nestedScrollView = (NestedScrollView) scroll;
    if (nestedScrollView.getScrollY() < verticalOffset || (!isSelected && isScrollToTop(target))) {
      nestedScrollView.scrollTo(0, verticalOffset);
    }
    if (isSelected && (nestedScrollView.getScrollY() < verticalOffset || verticalOffset == 0)) {
      nestedScrollView.scrollTo(0, 0);
      smoothAppBarLayout.syncOffset(0);
    }
  } else if (scroll instanceof RecyclerView) {
    RecyclerView recyclerView = (RecyclerView) scroll;
    boolean isAccuracy = recyclerView.getLayoutManager().findViewByPosition(ObservableRecyclerView.HEADER_VIEW_POSITION) != null;
    if (isAccuracy && recyclerView.computeVerticalScrollOffset() < verticalOffset) {
      recyclerView.scrollBy(0, verticalOffset - recyclerView.computeVerticalScrollOffset());
    } else if (!isSelected && isScrollToTop(target)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
    }
    if (isAccuracy && isSelected && (recyclerView.computeVerticalScrollOffset() < verticalOffset || verticalOffset == 0)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
      smoothAppBarLayout.syncOffset(0);
    }
  }
  return true;
}
 
Example 4
Source File: RecyclerViewFastScroller.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (scrollerView.isSelected()) return;
    int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
    int verticalScrollRange = recyclerView.computeVerticalScrollRange();
    float proportion = (float) verticalScrollOffset / ((float) verticalScrollRange - height);
    setScrollerHeight(height * proportion);
}
 
Example 5
Source File: ScrollUtils.java    From SlyceMessaging with MIT License 5 votes vote down vote up
private static int getOffsetOfGoal(int position, RecyclerView mRecyclerView, RecyclerView.Adapter mRecyclerAdapter) {
    int scrollPosition = mRecyclerView.computeVerticalScrollOffset();
    int goalPosition;
    if (position == 0) { // we are at the top
        goalPosition = 0;
    } else if (position == mRecyclerAdapter.getItemCount() - 1) {
        goalPosition = mRecyclerView.computeVerticalScrollRange();
    } else {
        goalPosition = 100000000; // this will cause scrollToPosition() to be called, which we want
    }
    return Math.abs(scrollPosition - goalPosition);
}
 
Example 6
Source File: OnScrollerGoDownListener.java    From FlowGeek with GNU General Public License v2.0 5 votes vote down vote up
@Override
    public final void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//            Log.d("thanatos", "toolbar translation y is "+ toolbar.getTranslationY());
        state.verticalOffset = recyclerView.computeVerticalScrollOffset();
        state.scrollingDiff = dy;
        int toolbarYOffset = (int) (dy + view.getTranslationY());
        ViewCompat.animate(view).cancel();
        if (state.scrollingDiff > 0) {
            if (toolbarYOffset < view.getHeight()) {
                if (state.verticalOffset > view.getHeight()) {
                    setElevation(TOOLBAR_ELEVATION);
                }
                view.setTranslationY(state.translationY = toolbarYOffset);
            } else {
                setElevation(0);
                view.setTranslationY(state.translationY = view.getHeight());
            }
        } else if (state.scrollingDiff < 0) {
            if (toolbarYOffset < 0) {
                if (state.verticalOffset <= 0) {
                    setElevation(0);
                }
                view.setTranslationY(state.translationY = 0);
            } else {
                if (state.verticalOffset > view.getHeight()) {
                    setElevation(TOOLBAR_ELEVATION);
                }
                view.setTranslationY(state.translationY = toolbarYOffset);
            }
        }
    }
 
Example 7
Source File: AlertController.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
    int scrollX = view.computeHorizontalScrollOffset();
    int scrollY = view.computeVerticalScrollOffset();
    onViewScroll(view, scrollX, scrollY, oldScrollX, oldScrollY);
    oldScrollX = scrollX;
    oldScrollY = scrollY;
}
 
Example 8
Source File: FastScroller.java    From IPTVFree with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
    if (bubble == null || handle.isSelected())
        return;
    final int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
    final int verticalScrollRange = recyclerView.computeVerticalScrollRange();
    float proportion = (float) verticalScrollOffset / ((float) verticalScrollRange - height);
    setBubbleAndHandlePosition(height * proportion);
}
 
Example 9
Source File: RecyclerViewUtils.java    From android-show-hide-toolbar with Apache License 2.0 5 votes vote down vote up
@Override
public final void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  state.verticalOffset = recyclerView.computeVerticalScrollOffset();
  state.scrollingOffset = dy;
  int toolbarYOffset = (int) (dy - toolbar.getTranslationY());
  toolbar.animate().cancel();
  if (state.scrollingOffset > 0) {
    if (toolbarYOffset < toolbar.getHeight()) {
      if (state.verticalOffset > toolbar.getHeight()) {
        toolbarSetElevation(TOOLBAR_ELEVATION);
      }
      toolbar.setTranslationY(state.translationY = -toolbarYOffset);
    } else {
      toolbarSetElevation(0);
      toolbar.setTranslationY(state.translationY = -toolbar.getHeight());
    }
  } else if (state.scrollingOffset < 0) {
    if (toolbarYOffset < 0) {
      if (state.verticalOffset <= 0) {
        toolbarSetElevation(0);
      }
      toolbar.setTranslationY(state.translationY = 0);
    } else {
      if (state.verticalOffset > toolbar.getHeight()) {
        toolbarSetElevation(TOOLBAR_ELEVATION);
      }
      toolbar.setTranslationY(state.translationY = -toolbarYOffset);
    }
  }
}
 
Example 10
Source File: RecyclerViewFastScroller.java    From Silence 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 11
Source File: RetractableToolbarUtil.java    From RetractableToolbar with Apache License 2.0 5 votes vote down vote up
@Override
public final void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    state.verticalOffset = recyclerView.computeVerticalScrollOffset();
    state.scrollingOffset = dy;
    int toolbarYOffset = (int) (dy - toolbar.getTranslationY());
    toolbar.animate().cancel();
    if (state.scrollingOffset > 0) {
        if (toolbarYOffset < toolbar.getHeight()) {
            if (state.verticalOffset > toolbar.getHeight()) {
                toolbarSetElevation(TOOLBAR_ELEVATION);
            }
            toolbar.setTranslationY(state.translationY = -toolbarYOffset);
        } else {
            toolbarSetElevation(0);
            toolbar.setTranslationY(state.translationY = -toolbar.getHeight());
        }
    } else if (state.scrollingOffset < 0) {
        if (toolbarYOffset < 0) {
            if (state.verticalOffset <= 0) {
                toolbarSetElevation(0);
            }
            toolbar.setTranslationY(state.translationY = 0);
        } else {
            if (state.verticalOffset > toolbar.getHeight()) {
                toolbarSetElevation(TOOLBAR_ELEVATION);
            }
            toolbar.setTranslationY(state.translationY = -toolbarYOffset);
        }
    }
}
 
Example 12
Source File: LoadMoreView.java    From PullRefreshLoadRecyclerView with MIT License 5 votes vote down vote up
private void computeOffsetWith(RecyclerView recyclerView) {
    if (recyclerView.getAdapter() == null) {
        setTranslationY(getMeasuredHeight());
        return;
    }
    int lastPosition = recyclerView.getAdapter().getItemCount() - 1;
    RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(lastPosition);
    if (viewHolder != null && viewHolder.itemView != null && viewHolder.itemView.isShown()) {
        int scrollRange = recyclerView.computeVerticalScrollRange();
        int currentScroll = recyclerView.computeVerticalScrollOffset();
        int rHeight = recyclerView.getHeight();
        int rPadTop = recyclerView.getPaddingTop();
        int offset = scrollRange - currentScroll - rHeight + getHeight() + rPadTop;

        checkAutoLoad();
        setTranslationY(offset);
        if (offset < 0) {
            dispatchOverScroll(offset, recyclerView.getScrollState());
        }
    } else if (recyclerView.getAdapter().getItemCount() == 0) {//no item
        setTranslationY(-recyclerView.getHeight() / 2 + getMeasuredHeight() / 2);
        checkAutoLoad();

    } else {
        setTranslationY(getMeasuredHeight());
    }
}
 
Example 13
Source File: RefreshView.java    From PullRefreshLoadRecyclerView with MIT License 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    if (recyclerView.getAdapter() == null) return;
    RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(0);
    if (viewHolder != null && viewHolder.itemView != null) {
        int currentScroll = recyclerView.computeVerticalScrollOffset();
        setTranslationY(-currentScroll - getHeight());

        dispatchOverScroll(-currentScroll, recyclerView.getScrollState());
    }
}
 
Example 14
Source File: GankFragment.java    From Demo_Public with MIT License 4 votes vote down vote up
private boolean isScrollToEnd(RecyclerView view){
    if (view == null) return false;
    if (view.computeVerticalScrollExtent() + view.computeVerticalScrollOffset() >= view.computeVerticalScrollRange())
        return true;
    return false;
}
 
Example 15
Source File: FastScroller.java    From APlayer with GNU General Public License v3.0 4 votes vote down vote up
private float getScrollProportion(RecyclerView recyclerView) {
  final int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
  final int verticalScrollRange = recyclerView.computeVerticalScrollRange();
  float proportion = (float) verticalScrollOffset / ((float) verticalScrollRange - mHeight);
  return mHeight * proportion;
}
 
Example 16
Source File: RecyclerViewUtil.java    From star-zone-android with Apache License 2.0 2 votes vote down vote up
/**
 * 判断recyclerview是否滑到底部
 * <p>
 * 原理:判断滑过的距离加上屏幕上的显示的区域是否比整个控件高度高
 *
 * @return
 */
public static boolean isScrollToBottom(@NonNull RecyclerView recyclerView) {
    return recyclerView != null && recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange();
}