Package 

Class RecyclerView.State


  • 
    public class RecyclerView.State
    
                        

    Contains useful information about the current RecyclerView state like target scrollposition or view focus. State object can also keep arbitrary data, identified by resourceids.

    Often times, RecyclerView components will need to pass information between each other.To provide a well defined data bus between components, RecyclerView passes the same Stateobject to component callbacks and these components can use it to exchange data.

    If you implement custom components, you can use State's put/get/remove methods to passdata between your components without needing to manage their lifecycles.

    • Method Summary

      Modifier and Type Method Description
      boolean isMeasuring() Returns true if the RecyclerView is currently measuring the layout.
      boolean isPreLayout() Returns true if the RecyclerView is in the pre-layout step where it is having its LayoutManager layout items where they will be at the beginning of a set ofpredictive item animations.
      boolean willRunPredictiveAnimations() Returns whether RecyclerView will run predictive animations in this layout passor not.
      boolean willRunSimpleAnimations() Returns whether RecyclerView will run simple animations in this layout passor not.
      void remove(int resourceId) Removes the mapping from the specified id, if there was any.
      <T> T get(int resourceId) Gets the Object mapped from the specified id, or nullif no such data exists.
      void put(int resourceId, Object data) Adds a mapping from the specified id to the specified value, replacing the previousmapping from the specified key if there was one.
      int getTargetScrollPosition() If scroll is triggered to make a certain item visible, this value will return theadapter index of that item.
      boolean hasTargetScrollPosition() Returns if current scroll has a target position.
      boolean didStructureChange()
      int getItemCount() Returns the total number of items that can be laid out.
      int getRemainingScrollHorizontal() Returns remaining horizontal scroll distance of an ongoing scroll animation(fling/smoothScrollTo/SmoothScroller) in pixels.
      int getRemainingScrollVertical() Returns remaining vertical scroll distance of an ongoing scroll animation(fling/smoothScrollTo/SmoothScroller) in pixels.
      String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isMeasuring

         boolean isMeasuring()

        Returns true if the RecyclerView is currently measuring the layout. This value is {@code true} only if the LayoutManager opted into the auto measure API and RecyclerViewhas non-exact measurement specs.

        Note that if the LayoutManager supports predictive animations and it is calculating thepre-layout step, this value will be {@code false} even if the RecyclerView is in {@code onMeasure} call. This is because pre-layout means the previous state of theRecyclerView and measurements made for that state cannot change the RecyclerView's size.LayoutManager is always guaranteed to receive another call to onLayoutChildren when this happens.

      • isPreLayout

         boolean isPreLayout()

        Returns true if the RecyclerView is in the pre-layout step where it is having its LayoutManager layout items where they will be at the beginning of a set ofpredictive item animations.

      • willRunPredictiveAnimations

         boolean willRunPredictiveAnimations()

        Returns whether RecyclerView will run predictive animations in this layout passor not.

      • willRunSimpleAnimations

         boolean willRunSimpleAnimations()

        Returns whether RecyclerView will run simple animations in this layout passor not.

      • remove

         void remove(int resourceId)

        Removes the mapping from the specified id, if there was any.

        Parameters:
        resourceId - Id of the resource you want to remove.
      • get

         <T> T get(int resourceId)

        Gets the Object mapped from the specified id, or nullif no such data exists.

        Parameters:
        resourceId - Id of the resource you want to remove.
      • put

         void put(int resourceId, Object data)

        Adds a mapping from the specified id to the specified value, replacing the previousmapping from the specified key if there was one.

        Parameters:
        resourceId - Id of the resource you want to add.
        data - The data you want to associate with the resourceId.
      • getTargetScrollPosition

         int getTargetScrollPosition()

        If scroll is triggered to make a certain item visible, this value will return theadapter index of that item.

      • getItemCount

         int getItemCount()

        Returns the total number of items that can be laid out. Note that this number is notnecessarily equal to the number of items in the adapter, so you should always use thisnumber for your position calculations and never access the adapter directly.

        RecyclerView listens for Adapter's notify events and calculates the effects of adapterdata changes on existing Views. These calculations are used to decide which animationsshould be run.

        To support predictive animations, RecyclerView may rewrite or reorder Adapter changes topresent the correct state to LayoutManager in pre-layout pass.

        For example, a newly added item is not included in pre-layout item count becausepre-layout reflects the contents of the adapter before the item is added. Behind thescenes, RecyclerView offsets getViewForPosition calls such thatLayoutManager does not know about the new item's existence in pre-layout. The item willbe available in second layout pass and will be included in the item count. Similaradjustments are made for moved and removed items as well.

        You can get the adapter's item count via getItemCount method.