Java Code Examples for androidx.recyclerview.widget.RecyclerView#OnScrollListener

The following examples show how to use androidx.recyclerview.widget.RecyclerView#OnScrollListener . 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: DynamicScrollUtils.java    From dynamic-support with Apache License 2.0 6 votes vote down vote up
/**
 * Set edge effect or glow color for recycler view.
 *
 * @param recyclerView The recycler view to set the edge effect color.
 * @param color The edge effect color to be set.
 * @param scrollListener Scroll listener to set color on over scroll.
 */
public static void setEdgeEffectColor(@Nullable RecyclerView recyclerView,
        final @ColorInt int color, @Nullable RecyclerView.OnScrollListener scrollListener) {
    if (recyclerView == null) {
        return;
    }

    if (scrollListener == null) {
        scrollListener =
                new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView,
                            int newState) {
                        super.onScrollStateChanged(recyclerView, newState);

                        setEdgeEffectColor(recyclerView, color);
                    }
                };

        recyclerView.removeOnScrollListener(scrollListener);
        recyclerView.addOnScrollListener(scrollListener);
    }

    setEdgeEffectColor(recyclerView, color);
}
 
Example 2
Source File: FastScroller.java    From FlexibleAdapter with Apache License 2.0 6 votes vote down vote up
protected void init() {
    if (isInitialized) {
        return;
    }
    isInitialized = true;
    setClipChildren(false);
    onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            if (!isEnabled() || bubble == null || handle.isSelected()) {
                return;
            }
            int verticalScrollOffset = recyclerView.computeVerticalScrollOffset();
            int verticalScrollRange = recyclerView.computeVerticalScrollRange();
            float proportion = (float) verticalScrollOffset / (float) (verticalScrollRange - height);
            setBubbleAndHandlePosition(height * proportion);
            // If scroll amount is small, don't show it
            if (minimumScrollThreshold == 0 || dy == 0 || Math.abs(dy) > minimumScrollThreshold || scrollbarAnimator.isAnimating()) {
                showScrollbar();
                autoHideScrollbar();
            }
        }
    };
}
 
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: RecyclerSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecyclerSpecOnBind() {
  EventHandler refreshHandler = mock(EventHandler.class);
  Binder<RecyclerView> binder = mock(Binder.class);

  SnapHelper snapHelper = mock(SnapHelper.class);

  final int size = 3;
  List<RecyclerView.OnScrollListener> scrollListeners = createListOfScrollListeners(size);

  LithoRecylerView.TouchInterceptor touchInterceptor =
      mock(LithoRecylerView.TouchInterceptor.class);

  RecyclerSpec.onBind(
      mComponentContext,
      mSectionsRecyclerView,
      binder,
      null,
      scrollListeners,
      snapHelper,
      true,
      touchInterceptor,
      refreshHandler);

  assertThat(mSectionsRecyclerView.isEnabled()).isTrue();

  assertThat(mSectionsRecyclerView.getRecyclerView()).isSameAs(mRecyclerView);

  verifyAddOnScrollListenerWasCalledNTimes(mRecyclerView, size);
  assertThat(mRecyclerView.getTouchInterceptor()).isSameAs(touchInterceptor);

  verify(binder).bind(mRecyclerView);
  assertThat(mRecyclerView.isLayoutRequested()).isTrue();
  assertThat(mSectionsRecyclerView.hasBeenDetachedFromWindow()).isFalse();
  verify(snapHelper).attachToRecyclerView(mRecyclerView);
}
 
Example 5
Source File: RecyclerSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecyclerSpecOnUnbind() {
  mSectionsRecyclerView.setHasBeenDetachedFromWindow(true);

  Binder<RecyclerView> binder = mock(Binder.class);

  final int size = 3;
  List<RecyclerView.OnScrollListener> scrollListeners = createListOfScrollListeners(size);

  RecyclerSpec.onUnbind(mComponentContext, mSectionsRecyclerView, binder, null, scrollListeners);

  verify(binder).unbind(mRecyclerView);
  verifyRemoveOnScrollListenerWasCalledNTimes(mRecyclerView, size);
  assertThat(mSectionsRecyclerView.getOnRefreshListener()).isNull();
}
 
Example 6
Source File: RecyclerSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
private static List<RecyclerView.OnScrollListener> createListOfScrollListeners(int size) {
  List<RecyclerView.OnScrollListener> onScrollListeners = new ArrayList<>(size);
  for (int i = 0; i < size; i++) {
    onScrollListeners.add(mock(RecyclerView.OnScrollListener.class));
  }

  return onScrollListeners;
}
 
Example 7
Source File: MaterialScrollBar.java    From MaterialScrollBar with Apache License 2.0 5 votes vote down vote up
protected void onDown(MotionEvent event) {
    if(indicator != null && indicator.getVisibility() == INVISIBLE && recyclerView.getAdapter() != null && !hiddenByNotEnoughElements) {
        indicator.setVisibility(VISIBLE);
        indicator.setAlpha(0F);
        indicator.animate().alpha(1F).setDuration(150).setListener(new AnimatorListenerAdapter() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                indicator.setAlpha(1F);
            }
        });
    }

    int top = handleThumb.getHeight() / 2;
    int bottom = recyclerView.getHeight() - Utils.getDP(72, recyclerView.getContext());
    float boundedY = Math.max(top, Math.min(bottom, event.getY() - getHandleOffset()));

    currentScrollPercent = (boundedY - top) / (bottom - top);
    if(isScrollChangeLargeEnoughForFastScroll(currentScrollPercent) ||
            currentScrollPercent == 0 || currentScrollPercent == 1) {
        previousScrollPercent = currentScrollPercent;
        int dy = scrollUtils.scrollToPositionAtProgress(currentScrollPercent);
        scrollUtils.scrollHandleAndIndicator();
        if(dy != 0) {
            for(RecyclerView.OnScrollListener listener : listeners) {
                listener.onScrolled(recyclerView, 0, dy);
            }
        }
    }

    if(lightOnTouch) {
        handleThumb.setBackgroundColor(handleColor);
    }
}
 
Example 8
Source File: MaterialScrollBar.java    From MaterialScrollBar with Apache License 2.0 4 votes vote down vote up
/**
 * Remove a listener for scroll events triggered by the scroll bar.
 */
public void removeScrollListener(RecyclerView.OnScrollListener scrollListener) {
    listeners.remove(scrollListener);
}
 
Example 9
Source File: UltimateRecyclerView.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
public void addOnScrollListener(RecyclerView.OnScrollListener customOnScrollListener) {
    mRecyclerView.addOnScrollListener(customOnScrollListener);
}
 
Example 10
Source File: SwipeListViewTouchListener.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
/**
     * Return ScrollListener for ListView
     *
     * @return OnScrollListener
     */
    public RecyclerView.OnScrollListener makeScrollListener() {
        return new RecyclerView.OnScrollListener() {

            private boolean isFirstItem = false;
            private boolean isLastItem = false;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                setEnabled(newState != recyclerView.SCROLL_STATE_DRAGGING);
                if (swipeClosesAllItemsWhenListMoves && newState == recyclerView.SCROLL_STATE_DRAGGING) {
                    closeOpenedItems();
                }
                if (newState == recyclerView.SCROLL_STATE_DRAGGING) {
                    listViewMoving = true;
                    setEnabled(false);
                }
                if (newState != recyclerView.SCROLL_STATE_SETTLING && newState != recyclerView.SCROLL_STATE_DRAGGING) {
                    listViewMoving = false;
                    downPosition = ListView.INVALID_POSITION;
                    swipeListView.resetScrolling();
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            setEnabled(true);
                        }
                    }, 500);
                }
            }

            @Override
            public void onScrolled(RecyclerView view, int dx, int dy) {
//                if (isFirstItem) {
//                    boolean onSecondItemList = firstVisibleItem == 1;
//                    if (onSecondItemList) {
//                        isFirstItem = false;
//                    }
//                } else {
//                    boolean onFirstItemList = firstVisibleItem == 0;
//                    if (onFirstItemList)
//                        isFirstItem = true;
//                        swipeListView.onFirstListItem();
//                    }
//                }
//                if (isLastItem) {
//                    boolean onBeforeLastItemList = firstVisibleItem + visibleItemCount == totalItemCount - 1;
//                    if (onBeforeLastItemList) {
//                        isLastItem = false;
//                    }
//                } else {
//                    boolean onLastItemList = firstVisibleItem + visibleItemCount >= totalItemCount;
//                    if (onLastItemList) {
//                        isLastItem = true;
//                        swipeListView.onLastListItem();
//                    }
//                }
            }
        };
    }
 
Example 11
Source File: UltimateRecyclerView.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
public void removeOnScrollListener(RecyclerView.OnScrollListener customOnScrollListener) {
    mRecyclerView.removeOnScrollListener(customOnScrollListener);
}
 
Example 12
Source File: ShadowRecyclerView.java    From materialistic with Apache License 2.0 4 votes vote down vote up
public RecyclerView.OnScrollListener getScrollListener() {
    return scrollListener;
}
 
Example 13
Source File: ShadowRecyclerView.java    From materialistic with Apache License 2.0 4 votes vote down vote up
public void removeOnScrollListener(RecyclerView.OnScrollListener listener) {
    if (scrollListener == listener) {
        scrollListener = null;
    }
}
 
Example 14
Source File: ShadowRecyclerView.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Implementation
public void addOnScrollListener(RecyclerView.OnScrollListener scrollListener) {
    this.scrollListener = scrollListener;
}
 
Example 15
Source File: RecyclerViewAttacher.java    From ScrollingPagerIndicator with Apache License 2.0 4 votes vote down vote up
@Override
public void attachToPager(@NonNull final ScrollingPagerIndicator indicator, @NonNull final RecyclerView pager) {
    if (!(pager.getLayoutManager() instanceof LinearLayoutManager)) {
        throw new IllegalStateException("Only LinearLayoutManager is supported");
    }
    if (pager.getAdapter() == null) {
        throw new IllegalStateException("RecyclerView has not Adapter attached");
    }
    this.layoutManager = (LinearLayoutManager) pager.getLayoutManager();
    this.recyclerView = pager;
    this.attachedAdapter = pager.getAdapter();
    this.indicator = indicator;

    dataObserver = new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            indicator.setDotCount(attachedAdapter.getItemCount());
            updateCurrentOffset();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            onChanged();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            onChanged();
        }
    };
    attachedAdapter.registerAdapterDataObserver(dataObserver);
    indicator.setDotCount(attachedAdapter.getItemCount());
    updateCurrentOffset();

    scrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE && isInIdleState()) {
                int newPosition = findCompletelyVisiblePosition();
                if (newPosition != RecyclerView.NO_POSITION) {
                    indicator.setDotCount(attachedAdapter.getItemCount());
                    if (newPosition < attachedAdapter.getItemCount()) {
                        indicator.setCurrentPosition(newPosition);
                    }
                }
            }
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            updateCurrentOffset();
        }
    };

    recyclerView.addOnScrollListener(scrollListener);
}
 
Example 16
Source File: ScrollingPauseLoadManager.java    From sketch with Apache License 2.0 4 votes vote down vote up
public void setOnScrollListener(RecyclerView.OnScrollListener recyclerScrollListener) {
    this.recyclerScrollListener = recyclerScrollListener;
}
 
Example 17
Source File: FloatingActionsMenu.java    From NewFastFrame with Apache License 2.0 4 votes vote down vote up
public void setOnScrollListener(RecyclerView.OnScrollListener onScrollListener) {
        mOnScrollListener = onScrollListener;
}
 
Example 18
Source File: TestLithoRecyclerView.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
public void removeOnScrollListener(RecyclerView.OnScrollListener onScrollListener) {
  removeOnScrollListeners.add(onScrollListener);
  super.removeOnScrollListener(onScrollListener);
}
 
Example 19
Source File: LoopBarView.java    From LoopBar with MIT License 2 votes vote down vote up
/**
 * Remove a listener from wrapped RecyclerView that was notified of any changes in scroll state or position.
 *
 * @param listener listener to set or null to clear
 */
@SuppressWarnings("unused")
public final void removeOnScrollListener(RecyclerView.OnScrollListener listener) {
    getRvCategories().removeOnScrollListener(listener);
}
 
Example 20
Source File: LoopBarView.java    From LoopBar with MIT License 2 votes vote down vote up
/**
 * Add a listener to wrapped RecyclerView that will be notified of any changes in scroll state or position.
 * <p>
 * <p>Components that add a listener should take care to remove it when finished.
 * Other components that take ownership of a view may call {@link #clearOnScrollListeners()}
 * to remove all attached listeners.</p>
 *
 * @param listener listener to set or null to clear
 */
@SuppressWarnings("unused")
public final void addOnScrollListener(RecyclerView.OnScrollListener listener) {
    getRvCategories().addOnScrollListener(listener);
}