androidx.recyclerview.widget.LinearSmoothScroller Java Examples

The following examples show how to use androidx.recyclerview.widget.LinearSmoothScroller. 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: SmoothScrollingLinearLayoutManager.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public void smoothScrollToPosition(@NonNull Context context, int position, float millisecondsPerInch) {
  final LinearSmoothScroller scroller = new LinearSmoothScroller(context) {
    @Override
    protected int getVerticalSnapPreference() {
      return LinearSmoothScroller.SNAP_TO_END;
    }

    @Override
    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
      return millisecondsPerInch / displayMetrics.densityDpi;
    }
  };

  scroller.setTargetPosition(position);
  startSmoothScroll(scroller);
}
 
Example #2
Source File: ContactRecyclerViewAdapter.java    From BaldPhone with Apache License 2.0 6 votes vote down vote up
public void changeCursor(@NonNull Cursor cursor) {
    this.cursor = cursor;
    applyToCursor();
    notifyDataSetChanged();

    if (getItemCount() > 0) {
        RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(activity) {
            @Override
            protected int getVerticalSnapPreference() {
                return LinearSmoothScroller.SNAP_TO_START;
            }
        };
        smoothScroller.setTargetPosition(0);
        recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
    }

}
 
Example #3
Source File: HierarchyFragment.java    From pandora with Apache License 2.0 6 votes vote down vote up
@Override
protected RecyclerView.LayoutManager onCreateLayoutManager() {
    return new LinearLayoutManager(Utils.getContext()) {
        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView,
                                           RecyclerView.State state, final int position) {
            LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    // let scroll smooth more
                    return 120f / displayMetrics.densityDpi;
                }

                @Override
                protected int getVerticalSnapPreference() {
                    return SNAP_TO_START;
                }
            };
            smoothScroller.setTargetPosition(position);
            startSmoothScroll(smoothScroller);
        }
    };
}
 
Example #4
Source File: SnappyLinearLayoutManager.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
                                   RecyclerView.State state,
                                   int position) {
    RecyclerView.SmoothScroller smoothScroller =
            new LinearSmoothScroller(recyclerView.getContext()) {
                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return SnappyLinearLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }

                @Override
                protected int getVerticalSnapPreference() {
                    return SNAP_TO_START; // override base class behavior
                }
            };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example #5
Source File: SnapUtil.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@link androidx.recyclerview.widget.RecyclerView.SmoothScroller} that takes snapping
 *     into account.
 */
public static RecyclerView.SmoothScroller getSmoothScrollerWithOffset(
    Context context, final int offset, final SmoothScrollAlignmentType type) {
  if (type == SmoothScrollAlignmentType.SNAP_TO_ANY
      || type == SmoothScrollAlignmentType.SNAP_TO_START
      || type == SmoothScrollAlignmentType.SNAP_TO_END) {
    final int snapPreference = type.getValue();
    return new EdgeSnappingSmoothScroller(context, snapPreference, offset);
  } else if (type == SmoothScrollAlignmentType.SNAP_TO_CENTER) {
    return new CenterSnappingSmoothScroller(context, offset);
  } else {
    return new LinearSmoothScroller(context);
  }
}
 
Example #6
Source File: GravitySnapHelper.java    From GravitySnapHelper with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public RecyclerView.SmoothScroller createScroller(RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)
            || recyclerView == null) {
        return null;
    }
    return new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView,
                                     RecyclerView.State state,
                                     RecyclerView.SmoothScroller.Action action) {
            if (recyclerView == null || recyclerView.getLayoutManager() == null) {
                // The associated RecyclerView has been removed so there is no action to take.
                return;
            }
            int[] snapDistances = calculateDistanceToFinalSnap(recyclerView.getLayoutManager(),
                    targetView);
            final int dx = snapDistances[0];
            final int dy = snapDistances[1];
            final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
            if (time > 0) {
                action.update(dx, dy, time, mDecelerateInterpolator);
            }
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return scrollMsPerInch / displayMetrics.densityDpi;
        }
    };
}
 
Example #7
Source File: SmoothCalendarLayoutManager.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(
    RecyclerView recyclerView, RecyclerView.State state, int position) {
  final LinearSmoothScroller linearSmoothScroller =
      new LinearSmoothScroller(recyclerView.getContext()) {

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
          return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
      };
  linearSmoothScroller.setTargetPosition(position);
  startSmoothScroll(linearSmoothScroller);
}
 
Example #8
Source File: SnappingLinearLayoutManager.java    From GetApk with MIT License 4 votes vote down vote up
public SnappingLinearLayoutManager(Context context) {
    this(context, LinearSmoothScroller.SNAP_TO_START);
}
 
Example #9
Source File: SnappingLinearLayoutManager.java    From GetApk with MIT License 4 votes vote down vote up
public SnappingLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    this(context, LinearSmoothScroller.SNAP_TO_START, orientation, reverseLayout);
}
 
Example #10
Source File: SnappingLinearLayoutManager.java    From GetApk with MIT License 4 votes vote down vote up
public SnappingLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    this(context, LinearSmoothScroller.SNAP_TO_START, attrs, defStyleAttr, defStyleRes);
}
 
Example #11
Source File: ViewPagerLayoutManager.java    From OmegaRecyclerView with MIT License 4 votes vote down vote up
private void startSmoothPendingScroll() {
    LinearSmoothScroller scroller = new DiscreteLinearSmoothScroller(mContext);
    scroller.setTargetPosition(calculateRealPosition(mCurrentPosition));
    startSmoothScroll(scroller);
}
 
Example #12
Source File: LayoutManager.java    From toktok-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void smoothScrollToPosition(@NonNull final RecyclerView recyclerView, RecyclerView.State state,
                                   final int position) {
    if (position < 0 || getItemCount() <= position) {
        Log.e("SuperSLiM.LayoutManager", "Ignored smooth scroll to " + position +
                " as it is not within the item range 0 - " + getItemCount());
        return;
    }

    // Temporarily disable sticky headers.
    requestLayout();

    recyclerView.getHandler().post(new Runnable() {
        @Override
        public void run() {
            LinearSmoothScroller smoothScroller = new LinearSmoothScroller(
                    recyclerView.getContext()) {
                @Override
                protected void onChildAttachedToWindow(View child) {
                    super.onChildAttachedToWindow(child);
                }

                @Override
                protected void onStop() {
                    super.onStop();
                    // Turn sticky headers back on.
                    requestLayout();
                }

                @Override
                protected int getVerticalSnapPreference() {
                    return LinearSmoothScroller.SNAP_TO_START;
                }

                @Override
                public int calculateDyToMakeVisible(@NonNull View view, int snapPreference) {
                    final RecyclerView.LayoutManager layoutManager = getLayoutManager();
                    if (!layoutManager.canScrollVertically()) {
                        return 0;
                    }
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
                            view.getLayoutParams();
                    final int top = layoutManager.getDecoratedTop(view) - params.topMargin;
                    final int bottom = layoutManager.getDecoratedBottom(view)
                            + params.bottomMargin;
                    final int start = getPosition(view) == 0 ? layoutManager.getPaddingTop()
                            : 0;
                    final int end = layoutManager.getHeight() - layoutManager
                            .getPaddingBottom();
                    int dy = calculateDtToFit(top, bottom, start, end, snapPreference);
                    return dy == 0 ? 1 : dy;
                }

                @Nullable
                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    if (getChildCount() == 0) {
                        return null;
                    }

                    return new PointF(0, getDirectionToPosition(targetPosition));
                }
            };
            smoothScroller.setTargetPosition(position);
            startSmoothScroll(smoothScroller);
        }
    });
}
 
Example #13
Source File: ImagePagerFragment.java    From Hentoid with Apache License 2.0 4 votes vote down vote up
private void initPager(View rootView) {
    adapter = new ImagePagerAdapter(requireContext());

    zoomFrame = requireViewById(rootView, R.id.image_viewer_zoom_frame);

    recyclerView = requireViewById(rootView, R.id.image_viewer_zoom_recycler);
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.addOnScrollListener(scrollListener);
    recyclerView.setOnGetMaxDimensionsListener(this::onGetMaxDimensions);
    recyclerView.requestFocus();
    recyclerView.setOnScaleListener(scale -> {
        if (pageSnapWidget != null && Preferences.Constant.PREF_VIEWER_ORIENTATION_HORIZONTAL == Preferences.getViewerOrientation()) {
            if (1.0 == scale && !pageSnapWidget.isPageSnapEnabled())
                pageSnapWidget.setPageSnapEnabled(true);
            else if (1.0 != scale && pageSnapWidget.isPageSnapEnabled())
                pageSnapWidget.setPageSnapEnabled(false);
        }
    });
    recyclerView.setLongTapListener(ev -> false);

    OnZoneTapListener onHorizontalZoneTapListener = new OnZoneTapListener(recyclerView)
            .setOnLeftZoneTapListener(this::onLeftTap)
            .setOnRightZoneTapListener(this::onRightTap)
            .setOnMiddleZoneTapListener(this::onMiddleTap);

    OnZoneTapListener onVerticalZoneTapListener = new OnZoneTapListener(recyclerView)
            .setOnMiddleZoneTapListener(this::onMiddleTap);

    recyclerView.setTapListener(onVerticalZoneTapListener);       // For paper roll mode (vertical)
    adapter.setItemTouchListener(onHorizontalZoneTapListener);    // For independent images mode (horizontal)

    adapter.setRecyclerView(recyclerView);

    llm = new PrefetchLinearLayoutManager(getContext());
    llm.setExtraLayoutSpace(10);
    recyclerView.setLayoutManager(llm);

    pageSnapWidget = new PageSnapWidget(recyclerView);

    smoothScroller = new LinearSmoothScroller(requireContext()) {
        @Override
        protected int getVerticalSnapPreference() {
            return LinearSmoothScroller.SNAP_TO_START;
        }
    };

    scrollListener.setOnStartOutOfBoundScrollListener(() -> {
        if (Preferences.isViewerContinuous()) previousBook();
    });
    scrollListener.setOnEndOutOfBoundScrollListener(() -> {
        if (Preferences.isViewerContinuous()) nextBook();
    });
}