androidx.recyclerview.widget.RecyclerView#scrollToPosition ( )源码实例Demo

下面列出了androidx.recyclerview.widget.RecyclerView#scrollToPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Aria2App   文件: DownloadCardsTutorial.java
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;
}
 
源代码2 项目: PretendYoureXyzzyAndroid   文件: GamesFragment.java
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();
    }
}
 
源代码3 项目: lbry-android   文件: Helper.java
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);
}
 
源代码4 项目: Mysplash   文件: BackToTopUtils.java
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);
    }
}
 
源代码5 项目: GestureViews   文件: FromRecyclerViewListener.java
@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);
    }
}
 
源代码6 项目: monero-wallet-android-app   文件: WeSwipeHelper.java
/**
 * 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);
        }
    }
}
 
源代码7 项目: Aria2App   文件: PeersServersTutorial.java
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;
}
 
源代码8 项目: Aria2App   文件: PeersServersTutorial.java
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;
}
 
源代码9 项目: Aria2App   文件: FilesTutorial.java
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;
}
 
源代码10 项目: Aria2App   文件: FoldersTutorial.java
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;
}
 
源代码11 项目: SearchPreference   文件: SearchPreferenceResult.java
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);
}
 
源代码12 项目: CrazyDaily   文件: ItemTouchHelperExtension.java
/**
 * 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);
        }
    }
}
 
源代码13 项目: aptoide-client-v8   文件: SearchResultFragment.java
@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);
}
 
源代码14 项目: aptoide-client-v8   文件: MyStoresFragment.java
@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);
}
 
源代码15 项目: android-test   文件: RecyclerViewActions.java
@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();
  }
}
 
源代码16 项目: CommonUtils   文件: OrderedRecyclerViewAdapter.java
private void scrollToTop() {
    RecyclerView recyclerView = getList();
    if (recyclerView != null) recyclerView.scrollToPosition(0);
}
 
源代码17 项目: Jockey   文件: RecyclerViewBindingAdapters.java
@BindingAdapter("scrollPosition")
public static void setScrollY(RecyclerView recyclerView, int scrollPosition) {
    if (getScrollPosition(recyclerView) != scrollPosition) {
        recyclerView.scrollToPosition(scrollPosition);
    }
}
 
源代码18 项目: android-test   文件: RecyclerViewActions.java
@Override
public void perform(UiController uiController, View view) {
  RecyclerView recyclerView = (RecyclerView) view;
  recyclerView.scrollToPosition(position);
  uiController.loopMainThreadUntilIdle();
}
 
源代码19 项目: Carbon   文件: ItemTouchHelper.java
/**
 * 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);
        }
    }
}
 
源代码20 项目: InfiniteScroll   文件: InfiniteScrollListener.java
/**
 * 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);
}