Package 

Class EpoxyModel


  • 
    public abstract class EpoxyModel<T>
    
                        

    Helper to bind data to a view using a builder style. The parameterized type should extend Android's View or EpoxyHolder.

    • Constructor Detail

      • EpoxyModel

        EpoxyModel()
    • Method Detail

      • buildView

         View buildView(@NonNull() ViewGroup parent)

        Create and return a new instance of a view for this model. By default a view is created byinflating the layout resource.

      • preBind

         void preBind(@NonNull() T view, @Nullable() EpoxyModel<out Object> previouslyBoundModel)

        Hook that is called before bind. This is similar to handlePreBind, but is intended forsubclasses of EpoxyModel to hook into rather than for generated code to hook into.Overriding preBind is useful to capture state before the view changes, e.g. for animations.

        Parameters:
        previouslyBoundModel - This is a model with the same id that was previously bound.
      • bind

         void bind(@NonNull() T view)

        Binds the current data to the given view. You should bind all fields including unset/emptyfields to ensure proper recycling.

      • bind

         void bind(@NonNull() T view, @NonNull() List<Object> payloads)

        Similar to bind, but provides a non null, non empty list of payloadsdescribing what changed. This is the payloads list specified in the adapter's notifyItemChangedmethod. This is a useful optimization to allow you to only change part of a view instead ofupdating the whole thing, which may prevent unnecessary layout calls. If there are no payloadsthen bind is called instead. This will only be used if the model is used withan EpoxyAdapter

      • bind

         void bind(@NonNull() T view, @NonNull() EpoxyModel<out Object> previouslyBoundModel)

        Similar to bind, but provides a non null model which was previously bound tothis view. This will only be called if the model is used with an EpoxyController.

        Parameters:
        previouslyBoundModel - This is a model with the same id that was previously bound.
      • unbind

         void unbind(@NonNull() T view)

        Called when the view bound to this model is recycled. Subclasses can override this if theirview should release resources when it's recycled.

        Note that bind can be called multiple times without an unbind call in betweenif the view has remained on screen to be reused across item changes. This means that you shouldnot rely on unbind to clear a view or model's state before bind is called again.

      • onVisibilityChanged

         void onVisibilityChanged(@FloatRange(from = 0.0f, to = 100.0f) float percentVisibleHeight, @FloatRange(from = 0.0f, to = 100.0f) float percentVisibleWidth, @Px() int visibleHeight, @Px() int visibleWidth, @NonNull() T view)

        TODO link to the wiki

      • id

         long id()
      • id

         EpoxyModel<T> id(long id)

        Override the default id in cases where the data subject naturally has an id, like an objectfrom a database. This id can only be set before the model is added to the adapter, it is anerror to change the id after that.

      • id

         EpoxyModel<T> id(@Nullable() Array<Number> ids)

        Use multiple numbers as the id for this model. Useful when you don't have a single long thatrepresents a unique id.

        This hashes the numbers, so there is a tiny risk of collision with other ids.

      • id

         EpoxyModel<T> id(long id1, long id2)

        Use two numbers as the id for this model. Useful when you don't have a single long thatrepresents a unique id.

        This hashes the two numbers, so there is a tiny risk of collision with other ids.

      • id

         EpoxyModel<T> id(@Nullable() CharSequence key)

        Use a string as the model id. Useful for models that don't clearly map to a numerical id. Thisis preferable to using hashCode because that is a 32 bit hash and this is a 64bit hash, giving better spread and less chance of collision with other ids.

        Since this uses a hashcode method to convert the String to a long there is a very small chancethat you may have a collision with another id. Assuming an even spread of hashcodes, andseveral hundred models in the adapter, there would be roughly 1 in 100 trillion chance of acollision. (http://preshing.com/20110504/hash-collision-probabilities/)

      • id

         EpoxyModel<T> id(@Nullable() CharSequence key, long id)

        Set an id that is namespaced with a string. This is useful when you need to show models ofmultiple types, side by side and don't want to risk id collisions.

        Since this uses a hashcode method to convert the String to a long there is a very small chancethat you may have a collision with another id. Assuming an even spread of hashcodes, andseveral hundred models in the adapter, there would be roughly 1 in 100 trillion chance of acollision. (http://preshing.com/20110504/hash-collision-probabilities/)

      • addIf

         void addIf(boolean condition, @NonNull() EpoxyController controller)

        Add this model to the given controller if the condition is true. Can only be called from inside buildModels.

      • getSpanSize

         int getSpanSize(int totalSpanCount, int position, int itemCount)

        Subclasses can override this if they want their view to take up more than one span in a gridlayout.

        Parameters:
        totalSpanCount - The number of spans in the grid
        position - The position of the model
        itemCount - The total number of items in the adapter
      • spanSize

         final int spanSize(int totalSpanCount, int position, int itemCount)

        Returns the actual span size of this model, using the SpanSizeOverrideCallback if onewas set, otherwise using the value from getSpanSize

      • isShown

         boolean isShown()

        Whether the model's view should be shown on screen. If false it won't be inflated and drawn,and will be like it was never added to the recycler view.

      • shouldSaveViewState

         boolean shouldSaveViewState()

        Whether the adapter should save the state of the view bound to this model.

      • onFailedToRecycleView

         boolean onFailedToRecycleView(@NonNull() T view)

        Called if the RecyclerView failed to recycle this model's view. You can take this opportunityto clear the animation(s) that affect the View's transient state and return trueso that the View can be recycled. Keep in mind that the View in question is already removedfrom the RecyclerView.