From 34df5f7db686e34962dc2e268c58df0828814b10 Mon Sep 17 00:00:00 2001 From: Nicholas Corder Date: Sat, 7 Jan 2017 23:30:20 -0800 Subject: [PATCH] Add delegation for `onFailToRecycleView()` Any child model has the ability to override `onFailToRecycleView()` to proform any manual cleanup as necessay. (usually caused by animations) Airbnb/epoxy#46 --- .../src/main/java/com/airbnb/epoxy/EpoxyAdapter.java | 8 ++++++++ .../src/main/java/com/airbnb/epoxy/EpoxyModel.java | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java index 08ef730d01..2f5d4758bf 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java @@ -2,6 +2,7 @@ package com.airbnb.epoxy; import android.os.Bundle; +import android.support.annotation.CallSuper; import android.support.annotation.Nullable; import android.support.v7.widget.GridLayoutManager.SpanSizeLookup; import android.support.v7.widget.RecyclerView; @@ -114,6 +115,7 @@ public int getItemCount() { return models.size(); } + public boolean isEmpty() { return models.isEmpty(); } @@ -166,6 +168,12 @@ protected void onModelBound(EpoxyViewHolder holder, EpoxyModel model, int pos } + @CallSuper + @Override + public boolean onFailedToRecycleView(EpoxyViewHolder holder) { + return holder.getModel().onFailedToRecycleView(); + } + protected BoundViewHolders getBoundViewHolders() { return boundViewHolders; } diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModel.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModel.java index 02e76af95c..33f1ee2c51 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModel.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModel.java @@ -1,7 +1,9 @@ package com.airbnb.epoxy; +import android.support.annotation.CallSuper; import android.support.annotation.LayoutRes; +import android.support.v7.widget.RecyclerView; import java.util.List; /** @@ -95,6 +97,16 @@ public final int getLayout() { return layout; } + /** + * Called when the view corresponding to this model failed to be recycled. This is left as a + * method stub since it is not always necessary to manually handle a failed recycle. + * @see EpoxyModel#onFailedToRecycleView() + */ + @CallSuper + protected boolean onFailedToRecycleView() { + return false; + } + /** * Sets fields of the model to default ones. */