-
Notifications
You must be signed in to change notification settings - Fork 728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify bind/unbind behavior. #96
Comments
You're right, working as intended but still a bit confusing. I believe that the Epoxy documentation does make sense in the context of RecyclerView. You said
So the view is only recycled when it goes off screen. It is not recycled when it is rebound to the same model or another model on screen. The I think it's a bit late now to rename the |
Thank you for the clarification about when a View is recycled.
Exactly this is the problematic part that caused my confusion about when a view is recycled. In most of the cases public class MainActivity extends AppCompatActivity {
int i = 0;
// with this delay unbind is always called. Around 200-300 it stops to call unbind and calls bind directly
private final long UPDATE_DELAY = 400;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView list = (RecyclerView) findViewById(R.id.list);
list.setLayoutManager(new LinearLayoutManager(this));
final TestAdapter adapter = new TestAdapter();
list.setAdapter(adapter);
final Handler h = new Handler(Looper.getMainLooper());
h.postDelayed(new Runnable() {
@Override
public void run() {
adapter.update("Val is : " + (i++));
h.postDelayed(this, UPDATE_DELAY);
}
}, 1000);
}
public class TestAdapter extends EpoxyAdapter {
public TestAdapter() {
enableDiffing();
}
public void update(String val) {
models.clear();
models.add(new TestModel(1l, val));
notifyModelsChanged();
}
}
public class TestModel extends EpoxyModel<LinearLayout> {
private final String val;
private TextView output;
public TestModel(long id, String val) {
super(id);
this.val = val;
}
@Override
protected int getDefaultLayout() {
return R.layout.list;
}
@Override
public int hashCode() {
return super.hashCode() + 31 * val.hashCode();
}
@Override
public void bind(LinearLayout view) {
super.bind(view);
output = new TextView(view.getContext());
output.setText(val);
view.addView(output);
}
//
@Override
public void unbind(LinearLayout view) {
super.unbind(view);
// this reverts the view into the initial state as long as the changes are not too frequent.
view.removeView(output);
}
}
} To keep the
|
I observed that
unbind
is not always called before a view is rebound to another model (recycled). I assume this is working as intended and not a bug. However I think the documentation on this behavior could be improved.Knowing the described behavior I can interpret this information into the documentation:
epoxy/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModel.java
Lines 39 to 60 in c2bdcd2
https://github.com/airbnb/epoxy/blob/c2bdcd29d8d6acce0d47a0a5f7c14b3b4f07d937/README.md
However without this knowledge this was absolutely not obvious for me from the documentation. Especially because of the naming of the two methods
bind
/unbind
and the observed behavior in most cases (most of the timeunbind
is called before the view is rebound).The text was updated successfully, but these errors were encountered: