Skip to content

Commit

Permalink
Clarify unbind behavior in documentation (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
elihart authored Feb 2, 2017
1 parent 6ddf5c2 commit 6919fcc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ Epoxy uses the layout resource id provided by `EpoxyModel#getLayout()` to create

Note, if you are [using view holders](#view-holders) then this process is slightly different. Instead, the adapter will pass the inflated view to your view holder and the view holder will be bound to the model.

Since RecyclerView reuses views when possible, a view may be bound multiple times. You should make sure that your usage of `EpoxyModel#bind(View)` completely updates the view according to the data in your model.

When the view is recycled, `EpoxyAdapter` will call `EpoxyModel#unbind(View)`, giving you a chance to release any resources associated with the view. This is a good opportunity to clear the view of large or expensive data such as bitmaps.

Since RecyclerView reuses views when possible, a view may be bound multiple times, without `unbind` necessarily being called in between. You should make sure that your usage of `EpoxyModel#bind(View)` completely updates the view according to the data in your model.

If the recycler view provided a non empty list of payloads with `onBindViewHolder(ViewHolder holder, int position, List<Object> payloads)`, then `EpoxyModel#bind(View, List<Object>)` will be called instead so that the model can be optimized to rebind according to what changed. This can help you prevent unnecessary layout changes if only part of the view changed.

## <a name="view-holders"/>Using View Holders
Expand Down
11 changes: 10 additions & 1 deletion epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ public void bind(T view, List<Object> payloads) {
bind(view);
}

/** Subclasses can override this if their view needs to release resources when it's recycled. */
/**
* Called when the view bound to this model is recycled. Subclasses can override this if their
* view should release resources when it's recycled.
* <p>
* Note that {@link #bind(Object)} can be called multiple times without an unbind call in between
* if the view has remained on screen to be reused across item changes. This means that you should
* not rely on unbind to clear a view or model's state before bind is called again.
*
* @see {@link EpoxyAdapter#onViewRecycled(EpoxyViewHolder)}
*/
public void unbind(T view) {
}

Expand Down

0 comments on commit 6919fcc

Please sign in to comment.