- Product requirements that are often encountered during development,
recycleview
Automatically scroll to a certain position. Specific scenarios may be as follows: - The page is automatically redirected to a certain location after initialization
- The page scrolls back to a certain location
- In order to show absolutely needed
recycleview
Make small position shifts and so on
In view of the above problems, we first look at recycleView has what method to provide us.
public void scrollToPosition(int position) {
if (mLayoutSuppressed) {
return;
}
stopScroll();
if (mLayout == null) {
Log.e(TAG, "Cannot scroll to position a LayoutManager set. "
+ "Call setLayoutManager with a non-null argument.");
return;
}
mLayout.scrollToPosition(position);
awakenScrollBars();
}
Copy the code
ScrollToPosition (int position) method that slides to the top of the item of the specified position.
public void smoothScrollToPosition(int position) {
if (mLayoutSuppressed) {
return;
}
if (mLayout == null) {
Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. "
+ "Call setLayoutManager with a non-null argument.");
return;
}
mLayout.smoothScrollToPosition(this, mState, position);
}
Copy the code
SmoothScrollToPosition (int Position) method same as above, but will have a smooth sliding effect.
public void scrollBy(int x, int y) { if (mLayout == null) { Log.e(TAG, "Cannot scroll without a LayoutManager set. " + "Call setLayoutManager with a non-null argument."); return; } if (mLayoutSuppressed) { return; } final boolean canScrollHorizontal = mLayout.canScrollHorizontally(); final boolean canScrollVertical = mLayout.canScrollVertically(); if (canScrollHorizontal || canScrollVertical) { scrollByInternal(canScrollHorizontal ? x : 0, canScrollVertical ? y : 0, null); }}Copy the code
The scrollBy(int x, int y) method slides by passing in an offset.
public void startSmoothScroll(SmoothScroller smoothScroller) { if (mSmoothScroller ! = null && smoothScroller ! = mSmoothScroller && mSmoothScroller.isRunning()) { mSmoothScroller.stop(); } mSmoothScroller = smoothScroller; mSmoothScroller.start(mRecyclerView, this); }Copy the code
Startsmoothscroller (SmoothScroller) Controls the movement of recycleView by passing in a SmoothScroller.
For the three cases mentioned above, this requirement involves several cases as follows:
- The target
item
Has appeared in the screen of the situation, this time to call method one, method two is not up to recycleView to move the location. At this point, we can choose to call method three to achieve the desired effect.
val smoothScroller: RecyclerView.SmoothScroller = object : LinearSmoothScroller(mContext) {
override fun getVerticalSnapPreference(): Int {
return SNAP_TO_START
}
}
smoothScroller.targetPosition = CIRCLE_POSITION + 1
mVirtualLayoutManager.startSmoothScroll(smoothScroller)
Copy the code
Of course, method 4 can also achieve the desired effect. At this time, what we need to work out is the required distance between our target item and the top
rv.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); . int toTop = rv.getChildAt(n).getTop(); rvProduct.scrollBy(0, top); . }});Copy the code
- When the target
item
Methods 1 and 2 will do the trick if they don’t appear on the screen.