belongs to Maven artifact androidx.car:car:1.0.0-alpha4

PagedSnapHelper

public class PagedSnapHelper
extends LinearSnapHelper

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.OnFlingListener
     ↳ androidx.recyclerview.widget.SnapHelper
       ↳ androidx.recyclerview.widget.LinearSnapHelper
         ↳ androidx.car.widget.PagedSnapHelper


Extension of a LinearSnapHelper that will snap to the start of the target child view to the start of the attached RecyclerView. The start of the view is defined as the top if the RecyclerView is scrolling vertically; it is defined as the left (or right if RTL) if the RecyclerView is scrolling horizontally.

Snapping may be disabled for views whose height is greater than that of the RecyclerView that contains them. In this case, the view will only be snapped to when it is first encountered. Otherwise, the user will be allowed to scroll freely through that view when it appears in the list. The snapping behavior will resume when the large view is scrolled off-screen.

Summary

Public constructors

PagedSnapHelper(Context context)

Public methods

void attachToRecyclerView(RecyclerView recyclerView)

Attaches the SnapHelper to the provided RecyclerView, by calling RecyclerView.setOnFlingListener(RecyclerView.OnFlingListener).

int[] calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, View targetView)

Override this method to snap to a particular point within the target view or the container view on any axis.

int[] calculateScrollDistance(int velocityX, int velocityY)

Calculate the estimated scroll distance in each direction given velocities on both axes.

View findSnapView(RecyclerView.LayoutManager layoutManager)

Finds the view to snap to.

boolean isAtEnd(RecyclerView.LayoutManager layoutManager)

Returns true if the RecyclerView is completely displaying the last item.

boolean isAtStart(RecyclerView.LayoutManager layoutManager)

Returns true if the RecyclerView is completely displaying the first item.

Protected methods

RecyclerView.SmoothScroller createScroller(RecyclerView.LayoutManager layoutManager)

Returns a scroller specific to this PagedSnapHelper.

Inherited methods

Public constructors

PagedSnapHelper

public PagedSnapHelper (Context context)

Parameters
context Context

Public methods

attachToRecyclerView

public void attachToRecyclerView (RecyclerView recyclerView)

Attaches the SnapHelper to the provided RecyclerView, by calling RecyclerView.setOnFlingListener(RecyclerView.OnFlingListener). You can call this method with null to detach it from the current RecyclerView.

Parameters
recyclerView RecyclerView: The RecyclerView instance to which you want to add this helper or null if you want to remove SnapHelper from the current RecyclerView.

calculateDistanceToFinalSnap

public int[] calculateDistanceToFinalSnap (RecyclerView.LayoutManager layoutManager, 
                View targetView)

Override this method to snap to a particular point within the target view or the container view on any axis.

This method is called when the SnapHelper has intercepted a fling and it needs to know the exact distance required to scroll by in order to snap to the target view.

Parameters
layoutManager RecyclerView.LayoutManager: the RecyclerView.LayoutManager associated with the attached RecyclerView

targetView View: the target view that is chosen as the view to snap

Returns
int[] the output coordinates the put the result into. out[0] is the distance on horizontal axis and out[1] is the distance on vertical axis.

calculateScrollDistance

public int[] calculateScrollDistance (int velocityX, 
                int velocityY)

Calculate the estimated scroll distance in each direction given velocities on both axes. This method will clamp the maximum scroll distance so that a single fling will never scroll more than one page.

Parameters
velocityX int: Fling velocity on the horizontal axis.

velocityY int: Fling velocity on the vertical axis.

Returns
int[] An array holding the calculated distances in x and y directions respectively.

findSnapView

public View findSnapView (RecyclerView.LayoutManager layoutManager)

Finds the view to snap to. The view to snap to is the child of the LayoutManager that is closest to the start of the RecyclerView. The "start" depends on if the LayoutManager is scrolling horizontally or vertically. If it is horizontally scrolling, then the start is the view on the left (right if RTL). Otherwise, it is the top-most view.

Parameters
layoutManager RecyclerView.LayoutManager: The current RecyclerView.LayoutManager for the attached RecyclerView.

Returns
View The View closest to the start of the RecyclerView. Returns nullwhen:
  • there is no item; or
  • no visible item can fully fit in the containing RecyclerView; or
  • an item longer than containing RecyclerView is about to scroll out.

isAtEnd

public boolean isAtEnd (RecyclerView.LayoutManager layoutManager)

Returns true if the RecyclerView is completely displaying the last item.

Parameters
layoutManager RecyclerView.LayoutManager

Returns
boolean

isAtStart

public boolean isAtStart (RecyclerView.LayoutManager layoutManager)

Returns true if the RecyclerView is completely displaying the first item.

Parameters
layoutManager RecyclerView.LayoutManager

Returns
boolean

Protected methods

createScroller

protected RecyclerView.SmoothScroller createScroller (RecyclerView.LayoutManager layoutManager)

Returns a scroller specific to this PagedSnapHelper. This scroller is used for all smooth scrolling operations, including flings.

Parameters
layoutManager RecyclerView.LayoutManager: The RecyclerView.LayoutManager associated with the attached RecyclerView.

Returns
RecyclerView.SmoothScroller a RecyclerView.SmoothScroller which will handle the scrolling.