Exemple #1
0
  private void goUp() {
    if (adapter.dataSet.posts != null) {

      int position = 0;
      int currentOrientation = getResources().getConfiguration().orientation;
      RecyclerView.SmoothScroller smoothScroller = null;
      if (rv.getLayoutManager() instanceof LinearLayoutManager
          && currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        position = ((LinearLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPosition();
        smoothScroller = new TopSnappedSmoothScroller(rv.getContext(), rv.getLayoutManager());

      } else if (rv.getLayoutManager() instanceof CatchStaggeredGridLayoutManager) {
        int[] firstVisibleItems = null;
        firstVisibleItems =
            ((CatchStaggeredGridLayoutManager) rv.getLayoutManager())
                .findFirstVisibleItemPositions(firstVisibleItems);
        if (firstVisibleItems != null && firstVisibleItems.length > 0) {
          position = firstVisibleItems[0];
          (rv.getLayoutManager())
              .smoothScrollToPosition(rv, new RecyclerView.State(), position - 1);
          return;
        }
      } else {
        position =
            ((PreCachingLayoutManager) rv.getLayoutManager())
                .findFirstCompletelyVisibleItemPosition();
        smoothScroller = new TopSnappedSmoothScroller(rv.getContext(), rv.getLayoutManager());
      }

      if (smoothScroller != null) {
        smoothScroller.setTargetPosition(position - 1);
        (rv.getLayoutManager()).startSmoothScroll(smoothScroller);
      }
    }
  }
 @Override
 public void smoothScrollToPosition(
     RecyclerView recyclerView, RecyclerView.State state, int position) {
   RecyclerView.SmoothScroller smoothScroller =
       new TopSnappedSmoothScroller(recyclerView.getContext());
   smoothScroller.setTargetPosition(position);
   startSmoothScroll(smoothScroller);
 }