Java Code Examples for androidx.recyclerview.widget.RecyclerView#scrollToPosition()
The following examples show how to use
androidx.recyclerview.widget.RecyclerView#scrollToPosition() .
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: DownloadCardsTutorial.java From Aria2App with GNU General Public License v3.0 | 7 votes |
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; }
Example 2
Source File: Helper.java From lbry-android with MIT License | 6 votes |
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); }
Example 3
Source File: FromRecyclerViewListener.java From GestureViews with Apache License 2.0 | 6 votes |
@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); } }
Example 4
Source File: BackToTopUtils.java From Mysplash with GNU Lesser General Public License v3.0 | 6 votes |
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); } }
Example 5
Source File: GamesFragment.java From PretendYoureXyzzyAndroid with GNU General Public License v3.0 | 6 votes |
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(); } }
Example 6
Source File: FoldersTutorial.java From Aria2App with GNU General Public License v3.0 | 5 votes |
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; }
Example 7
Source File: FilesTutorial.java From Aria2App with GNU General Public License v3.0 | 5 votes |
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; }
Example 8
Source File: SearchPreferenceResult.java From SearchPreference with MIT License | 5 votes |
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); }
Example 9
Source File: PeersServersTutorial.java From Aria2App with GNU General Public License v3.0 | 5 votes |
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; }
Example 10
Source File: ItemTouchHelperExtension.java From CrazyDaily with Apache License 2.0 | 5 votes |
/** * 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); } } }
Example 11
Source File: WeSwipeHelper.java From monero-wallet-android-app with MIT License | 5 votes |
/** * 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); } } }
Example 12
Source File: SearchResultFragment.java From aptoide-client-v8 with GNU General Public License v3.0 | 5 votes |
@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); }
Example 13
Source File: MyStoresFragment.java From aptoide-client-v8 with GNU General Public License v3.0 | 5 votes |
@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); }
Example 14
Source File: PeersServersTutorial.java From Aria2App with GNU General Public License v3.0 | 5 votes |
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; }
Example 15
Source File: RecyclerViewActions.java From android-test with Apache License 2.0 | 5 votes |
@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(); } }
Example 16
Source File: RecyclerViewBindingAdapters.java From Jockey with Apache License 2.0 | 4 votes |
@BindingAdapter("scrollPosition") public static void setScrollY(RecyclerView recyclerView, int scrollPosition) { if (getScrollPosition(recyclerView) != scrollPosition) { recyclerView.scrollToPosition(scrollPosition); } }
Example 17
Source File: RecyclerViewActions.java From android-test with Apache License 2.0 | 4 votes |
@Override public void perform(UiController uiController, View view) { RecyclerView recyclerView = (RecyclerView) view; recyclerView.scrollToPosition(position); uiController.loopMainThreadUntilIdle(); }
Example 18
Source File: OrderedRecyclerViewAdapter.java From CommonUtils with Apache License 2.0 | 4 votes |
private void scrollToTop() { RecyclerView recyclerView = getList(); if (recyclerView != null) recyclerView.scrollToPosition(0); }
Example 19
Source File: ItemTouchHelper.java From Carbon with Apache License 2.0 | 3 votes |
/** * 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); } } }
Example 20
Source File: InfiniteScrollListener.java From InfiniteScroll with Apache License 2.0 | 2 votes |
/** * 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); }