下面列出了androidx.recyclerview.widget.RecyclerView#scrollToPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean buildSequence(@NonNull RecyclerView list) {
LinearLayoutManager llm = (LinearLayoutManager) list.getLayoutManager();
if (llm == null) return false;
int pos = llm.findFirstCompletelyVisibleItemPosition();
if (pos == -1) pos = 0;
DownloadCardsAdapter.ViewHolder holder = (DownloadCardsAdapter.ViewHolder) list.findViewHolderForLayoutPosition(pos);
if (holder != null) {
list.scrollToPosition(pos);
add(forView(holder.donutProgress, R.string.tutorial_moreDetails)
.fitSystemWindows(true)
.focusShape(FocusShape.CIRCLE)
.enableAutoTextPosition());
add(forView(holder.more, R.string.tutorial_evenMoreDetails)
.fitSystemWindows(true)
.focusShape(FocusShape.ROUNDED_RECTANGLE)
.roundRectRadius(8)
.enableAutoTextPosition());
return true;
}
return false;
}
public void viewGame(int gid, boolean locked) {
if (adapter == null) return;
if (locked && adapter.doesFilterOutLockedLobbies()) {
Prefs.putBoolean(PK.FILTER_LOCKED_LOBBIES, false);
adapter.setFilterOutLockedLobbies(false);
}
int pos = Utils.indexOf(adapter.getVisibleGames(), gid);
if (pos != -1) {
RecyclerView list = rmv.list();
list.scrollToPosition(pos);
RecyclerView.ViewHolder holder = list.findViewHolderForAdapterPosition(pos);
if (holder instanceof GamesAdapter.ViewHolder)
((GamesAdapter.ViewHolder) holder).expand.performClick();
}
}
public static void refreshRecyclerView(RecyclerView rv) {
if (rv == null) {
return;
}
RecyclerView.Adapter adapter = rv.getAdapter();
int prevScrollPosition = 0;
RecyclerView.LayoutManager lm = rv.getLayoutManager();
if (lm instanceof LinearLayoutManager) {
prevScrollPosition = ((LinearLayoutManager) lm).findLastCompletelyVisibleItemPosition();
} else if (lm instanceof GridLayoutManager) {
prevScrollPosition = ((GridLayoutManager) lm).findLastCompletelyVisibleItemPosition();
}
rv.setAdapter(null);
rv.setAdapter(adapter);
rv.scrollToPosition(prevScrollPosition > 0 ? prevScrollPosition : 0);
}
public static void scrollToTop(RecyclerView recyclerView) {
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
int firstVisibleItemPosition = 0;
if (manager instanceof LinearLayoutManager) {
firstVisibleItemPosition = getFirstVisibleItemPosition((LinearLayoutManager) manager);
} else if (manager instanceof StaggeredGridLayoutManager) {
firstVisibleItemPosition = getFirstVisibleItemPosition((StaggeredGridLayoutManager) manager);
}
if (firstVisibleItemPosition > 5) {
recyclerView.scrollToPosition(5);
}
recyclerView.smoothScrollToPosition(0);
MysplashActivity activity = MysplashApplication.getInstance().getTopActivity();
if (activity != null) {
ComponentFactory.getSettingsService().notifySetBackToTop(activity);
}
}
@Override
void scrollToPosition(RecyclerView list, int pos) {
if (list.getLayoutManager() instanceof LinearLayoutManager) {
// Centering item in its parent
final LinearLayoutManager manager = (LinearLayoutManager) list.getLayoutManager();
final boolean isHorizontal = manager.getOrientation() == LinearLayoutManager.HORIZONTAL;
int offset = isHorizontal
? (list.getWidth() - list.getPaddingLeft() - list.getPaddingRight()) / 2
: (list.getHeight() - list.getPaddingTop() - list.getPaddingBottom()) / 2;
final RecyclerView.ViewHolder holder = list.findViewHolderForAdapterPosition(pos);
if (holder != null) {
final View view = holder.itemView;
offset -= isHorizontal ? view.getWidth() / 2 : view.getHeight() / 2;
}
manager.scrollToPositionWithOffset(pos, offset);
} else {
list.scrollToPosition(pos);
}
}
/**
* Called when {@link #onMove(RecyclerView, RecyclerView.ViewHolder, RecyclerView.ViewHolder)} returns true.
* <p>
* WeSwipeHelper does not create an extra Bitmap or View while dragging, instead, it
* modifies the existing View. Because of this reason, it is important that the View is
* still part of the layout after it is moved. This may not work as intended when swapped
* Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views
* which were not eligible for dropping over).
* <p>
* This method is responsible to give necessary hint to the LayoutManager so that it will
* keep the View in visible area. For example, for LinearLayoutManager, this is as simple
* as calling {@link LinearLayoutManager#scrollToPositionWithOffset(int, int)}.
* <p>
* Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's
* new position is likely to be out of bounds.
* <p>
* It is important to ensure the ViewHolder will stay visible as otherwise, it might be
* removed by the LayoutManager if the move causes the View to go out of bounds. In that
* case, drag will end prematurely.
*
* @param recyclerView The RecyclerView controlled by the WeSwipeHelper.
* @param viewHolder The ViewHolder under user's control.
* @param fromPos The previous adapter position of the dragged item (before it was
* moved).
* @param target The ViewHolder on which the currently active item has been dropped.
* @param toPos The new adapter position of the dragged item.
* @param x The updated left value of the dragged View after drag translations
* are applied. This value does not include margins added by
* {@link RecyclerView.ItemDecoration}s.
* @param y The updated top value of the dragged View after drag translations
* are applied. This value does not include margins added by
* {@link RecyclerView.ItemDecoration}s.
*/
public void onMoved(final RecyclerView recyclerView,
final RecyclerView.ViewHolder viewHolder, int fromPos, final RecyclerView.ViewHolder target, int toPos, int x,
int y) {
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof WeSwipeHelper.ViewDropHandler) {
((WeSwipeHelper.ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView,
target.itemView, x, y);
return;
}
// if layout manager cannot handle it, do some guesswork
if (layoutManager.canScrollHorizontally()) {
final int minLeft = layoutManager.getDecoratedLeft(target.itemView);
if (minLeft <= recyclerView.getPaddingLeft()) {
recyclerView.scrollToPosition(toPos);
}
final int maxRight = layoutManager.getDecoratedRight(target.itemView);
if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
recyclerView.scrollToPosition(toPos);
}
}
if (layoutManager.canScrollVertically()) {
final int minTop = layoutManager.getDecoratedTop(target.itemView);
if (minTop <= recyclerView.getPaddingTop()) {
recyclerView.scrollToPosition(toPos);
}
final int maxBottom = layoutManager.getDecoratedBottom(target.itemView);
if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
recyclerView.scrollToPosition(toPos);
}
}
}
public final boolean buildForServers(@NonNull RecyclerView list) {
RecyclerView.ViewHolder holder = list.findViewHolderForLayoutPosition(0);
if (holder != null) {
list.scrollToPosition(0);
add(forView(holder.itemView, R.string.tutorial_serverDetails)
.enableAutoTextPosition()
.roundRectRadius(8)
.focusShape(FocusShape.ROUNDED_RECTANGLE));
return true;
}
return false;
}
public final boolean buildForPeers(@NonNull RecyclerView list) {
RecyclerView.ViewHolder holder = list.findViewHolderForLayoutPosition(0);
if (holder != null) {
list.scrollToPosition(0);
add(forView(holder.itemView, R.string.tutorial_peerDetails)
.enableAutoTextPosition()
.roundRectRadius(8)
.focusShape(FocusShape.ROUNDED_RECTANGLE));
return true;
}
return false;
}
public final boolean buildSequence(@NonNull RecyclerView list, @Nullable AriaDirectory dir) {
int firstFile = dir == null ? 0 : dir.dirs.size();
RecyclerView.ViewHolder holder = list.findViewHolderForLayoutPosition(firstFile);
if (holder != null) {
list.scrollToPosition(firstFile);
add(forView(holder.itemView, R.string.tutorial_fileDetails)
.enableAutoTextPosition()
.roundRectRadius(8)
.focusShape(FocusShape.ROUNDED_RECTANGLE));
return true;
}
return false;
}
public final boolean buildSequence(@NonNull RecyclerView list) {
RecyclerView.ViewHolder holder = list.findViewHolderForLayoutPosition(0);
if (holder != null) {
list.scrollToPosition(0);
add(forView(holder.itemView, R.string.tutorial_folderDetails)
.enableAutoTextPosition()
.roundRectRadius(8)
.focusShape(FocusShape.ROUNDED_RECTANGLE));
return true;
}
return false;
}
private void doHighlight(final PreferenceFragmentCompat prefsFragment) {
final Preference prefResult = prefsFragment.findPreference(getKey());
if (prefResult == null) {
Log.e("doHighlight", "Preference not found on given screen");
return;
}
final RecyclerView recyclerView = prefsFragment.getListView();
final RecyclerView.Adapter<?> adapter = recyclerView.getAdapter();
if (adapter instanceof PreferenceGroup.PreferencePositionCallback) {
PreferenceGroup.PreferencePositionCallback callback = (PreferenceGroup.PreferencePositionCallback) adapter;
final int position = callback.getPreferenceAdapterPosition(prefResult);
if (position != RecyclerView.NO_POSITION) {
recyclerView.scrollToPosition(position);
recyclerView.postDelayed(() -> {
RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(position);
if (holder != null) {
Drawable background = holder.itemView.getBackground();
if (Build.VERSION.SDK_INT >= 21 && background instanceof RippleDrawable) {
forceRippleAnimation((RippleDrawable) background);
return;
}
}
highlightFallback(prefsFragment, prefResult);
}, 200);
return;
}
}
highlightFallback(prefsFragment, prefResult);
}
/**
* Called when {@link #onMove(RecyclerView, ViewHolder, ViewHolder)} returns true.
* <p>
* ItemTouchHelper does not create an extra Bitmap or View while dragging, instead, it
* modifies the existing View. Because of this reason, it is important that the View is
* still part of the layout after it is moved. This may not work as intended when swapped
* Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views
* which were not eligible for dropping over).
* <p>
* This method is responsible to give necessary hint to the LayoutManager so that it will
* keep the View in visible area. For example, for LinearLayoutManager, this is as simple
* <p>
* <p>
* Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's
* new position is likely to be out of bounds.
* <p>
* It is important to ensure the ViewHolder will stay visible as otherwise, it might be
* removed by the LayoutManager if the move causes the View to go out of bounds. In that
* case, drag will end prematurely.
*
* @param recyclerView The RecyclerView controlled by the ItemTouchHelper.
* @param viewHolder The ViewHolder under user's control.
* @param fromPos The previous adapter position of the dragged item (before it was
* moved).
* @param target The ViewHolder on which the currently active item has been dropped.
* @param toPos The new adapter position of the dragged item.
* @param x The updated left value of the dragged View after drag translations
* are applied. This value does not include margins added by
* {@link RecyclerView.ItemDecoration}s.
* @param y The updated top value of the dragged View after drag translations
* are applied. This value does not include margins added by
* {@link RecyclerView.ItemDecoration}s.
*/
public void onMoved(final RecyclerView recyclerView,
final ViewHolder viewHolder, int fromPos, final ViewHolder target, int toPos, int x,
int y) {
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof ViewDropHandler) {
((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView,
target.itemView, x, y);
return;
}
// if layout manager cannot handle it, do some guesswork
if (layoutManager.canScrollHorizontally()) {
final int minLeft = layoutManager.getDecoratedLeft(target.itemView);
if (minLeft <= recyclerView.getPaddingLeft()) {
recyclerView.scrollToPosition(toPos);
}
final int maxRight = layoutManager.getDecoratedRight(target.itemView);
if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
recyclerView.scrollToPosition(toPos);
}
}
if (layoutManager.canScrollVertically()) {
final int minTop = layoutManager.getDecoratedTop(target.itemView);
if (minTop <= recyclerView.getPaddingTop()) {
recyclerView.scrollToPosition(toPos);
}
final int maxBottom = layoutManager.getDecoratedBottom(target.itemView);
if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
recyclerView.scrollToPosition(toPos);
}
}
}
@Override public void scrollToTop() {
RecyclerView list;
if (followedStoresResultList.getVisibility() == View.VISIBLE) {
list = followedStoresResultList;
} else {
list = allStoresResultList;
}
LinearLayoutManager layoutManager = ((LinearLayoutManager) list.getLayoutManager());
int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
if (lastVisibleItemPosition > 10) {
list.scrollToPosition(10);
}
list.smoothScrollToPosition(0);
}
@Override @UiThread public void scrollToTop() {
RecyclerView view = getRecyclerView();
LinearLayoutManager layoutManager = ((LinearLayoutManager) view.getLayoutManager());
int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
if (lastVisibleItemPosition > 10) {
view.scrollToPosition(10);
}
view.smoothScrollToPosition(0);
}
@SuppressWarnings("unchecked")
@Override
public void perform(UiController uiController, View view) {
RecyclerView recyclerView = (RecyclerView) view;
try {
int maxMatches = atPosition == NO_POSITION ? 2 : atPosition + 1;
int selectIndex = atPosition == NO_POSITION ? 0 : atPosition;
List<MatchedItem> matchedItems = itemsMatching(recyclerView, viewHolderMatcher, maxMatches);
if (selectIndex >= matchedItems.size()) {
throw new RuntimeException(
String.format(
"Found %d items matching %s, but position %d was requested.",
matchedItems.size(), viewHolderMatcher.toString(), atPosition));
}
if (atPosition == NO_POSITION && matchedItems.size() == 2) {
StringBuilder ambiguousViewError = new StringBuilder();
ambiguousViewError.append(
String.format("Found more than one sub-view matching %s", viewHolderMatcher));
for (MatchedItem item : matchedItems) {
ambiguousViewError.append(item + "\n");
}
throw new RuntimeException(ambiguousViewError.toString());
}
recyclerView.scrollToPosition(matchedItems.get(selectIndex).position);
uiController.loopMainThreadUntilIdle();
} catch (RuntimeException e) {
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(e)
.build();
}
}
private void scrollToTop() {
RecyclerView recyclerView = getList();
if (recyclerView != null) recyclerView.scrollToPosition(0);
}
@BindingAdapter("scrollPosition")
public static void setScrollY(RecyclerView recyclerView, int scrollPosition) {
if (getScrollPosition(recyclerView) != scrollPosition) {
recyclerView.scrollToPosition(scrollPosition);
}
}
@Override
public void perform(UiController uiController, View view) {
RecyclerView recyclerView = (RecyclerView) view;
recyclerView.scrollToPosition(position);
uiController.loopMainThreadUntilIdle();
}
/**
* Called when {@link #onMove(RecyclerView, ViewHolder, ViewHolder)} returns true.
* <p>
* ItemTouchHelper does not create an extra Bitmap or View while dragging, instead, it
* modifies the existing View. Because of this reason, it is important that the View is
* still part of the layout after it is moved. This may not work as intended when swapped
* Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views
* which were not eligible for dropping over).
* <p>
* This method is responsible to give necessary hint to the LayoutManager so that it will
* keep the View in visible area. For example, for LinearLayoutManager, this is as simple as
* calling {@link LinearLayoutManager#scrollToPositionWithOffset(int, int)}.
* <p>
* Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's new
* position is likely to be out of bounds.
* <p>
* It is important to ensure the ViewHolder will stay visible as otherwise, it might be
* removed by the LayoutManager if the move causes the View to go out of bounds. In that
* case, drag will end prematurely.
*
* @param recyclerView The RecyclerView controlled by the ItemTouchHelper.
* @param viewHolder The ViewHolder under user's control.
* @param fromPos The previous adapter position of the dragged item (before it was
* moved).
* @param target The ViewHolder on which the currently active item has been dropped.
* @param toPos The new adapter position of the dragged item.
* @param x The updated left value of the dragged View after drag translations
* are applied. This value does not include margins added by {@link
* RecyclerView.ItemDecoration}s.
* @param y The updated top value of the dragged View after drag translations are
* applied. This value does not include margins added by {@link
* RecyclerView.ItemDecoration}s.
*/
public void onMoved(final RecyclerView recyclerView,
final ViewHolder viewHolder, int fromPos, final ViewHolder target, int toPos, int x,
int y) {
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof ViewDropHandler) {
((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView,
target.itemView, x, y);
return;
}
// if layout manager cannot handle it, do some guesswork
if (layoutManager.canScrollHorizontally()) {
final int minLeft = layoutManager.getDecoratedLeft(target.itemView);
if (minLeft <= recyclerView.getPaddingLeft()) {
recyclerView.scrollToPosition(toPos);
}
final int maxRight = layoutManager.getDecoratedRight(target.itemView);
if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
recyclerView.scrollToPosition(toPos);
}
}
if (layoutManager.canScrollVertically()) {
final int minTop = layoutManager.getDecoratedTop(target.itemView);
if (minTop <= recyclerView.getPaddingTop()) {
recyclerView.scrollToPosition(toPos);
}
final int maxBottom = layoutManager.getDecoratedBottom(target.itemView);
if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
recyclerView.scrollToPosition(toPos);
}
}
}
/**
* Refreshes RecyclerView by setting new adapter,
* calling invalidate method and scrolling to given position
*
* @param view RecyclerView to be refreshed
* @param adapter adapter with new list of items to be loaded
* @param position position to which RecyclerView will be scrolled
*/
protected void refreshView(RecyclerView view, RecyclerView.Adapter adapter, int position) {
view.setAdapter(adapter);
view.invalidate();
view.scrollToPosition(position);
}