Skip to content
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

Add support for nested RecyclerView to EpoxyVisibilityTracker - Part 2 #633

Merged
merged 23 commits into from
Jan 14, 2019

Conversation

eboudrant
Copy link
Contributor

@eboudrant eboudrant commented Dec 10, 2018

This is part 2 :

  • Auto register EpoxyVisibilityTracker on any nested recycler view.
  • Any parent visibility event are forwarded to the nested EpoxyVisibilityTracker.

Let's discuss the implementation and I will add unit tests later...

Copy link
Contributor

@elihart elihart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eboudrant nice - did an initial pass with some thoughts. Generally the approach is good I think, but have some ideas on how to clean it up potentially.

public void onChildAttachedToWindow(@NonNull View child) {
if (child instanceof RecyclerView) {
RecyclerView recyclerView = (RecyclerView) child;
if (recyclerView.getAdapter() instanceof BaseEpoxyAdapter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is if (recyclerView.getAdapter() instanceof BaseEpoxyAdapter) { necessary? if the child has had attach called on it then it will work, otherwise it is a no-op, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here was if the child is a RecyclerView which is not managed by epoxy there is no point to have visibility tracking. But that's true that the same check is in EpoxyVisibilityTracker.registerNestedRecyclerView ... we could remove it from here. This way no need to hookup onChildAttachedToWindow method.

When you say attach do you mean EpoxyVisibilityTracker.attach ? Because the idea of this implementation was to automatically attach nested RecyclerView. May be it is a bad idea and it would be better to have user explicitly call EpoxyVisibilityTracker.attach for nested RecyclerViews.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we could use a OnChildAttachStateChangeListener to automatically attach nested visibility trackers from the parent. Not sure what is the best, auto-attach nested or let the the developer attach them ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So as discussed I changed to use OnChildAttachStateChangeListener which is simpler.
Thanks @elihart

…ster it manually. Via a `OnAttachStateChangeListener` we will lookup for the parent to eventually register the nested tracker to the parent tracker.
Copy link
Contributor

@elihart elihart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, have been on vacation for three weeks!

The description still mentions a manual method registerNestedRecyclerView - I think that was removed in favor of complete automation?

The changes look much cleaner with the tag approach :) I am a bit confused by how it works though - for the OnAttachStateChangeListener to be added to a nested recycler view we need to call attach on it, which is still manual.

Can we get rid of the new OnAttachStateChangeListener completely, and instead use the existing OnChildAttachStateChangeListener#onChildViewAttachedToWindow in the recyclerview on line 340?

So when we call attach on a recyclerview, we add a listener for child views being added to the recyclerview - in the listener's onChildViewAttachedToWindow we can check if the view is a RecyclerView, and if so it is a nested RV and we can use your existing logic.

@elihart
Copy link
Contributor

elihart commented Jan 4, 2019

@eboudrant this looks great to me - do you mind adding some tests before I merge?

thanks!

Copy link
Contributor

@elihart elihart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, thanks for this!

* @param recyclerView the recycler view
* @return true if managed by an {@link BaseEpoxyAdapter}
*/
private boolean notEpoxyManaged(RecyclerView recyclerView) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import com.airbnb.epoxy.ModelView
import com.airbnb.epoxy.ModelView.Size

@ModelView(saveViewState = true, autoLayout = Size.MATCH_WIDTH_WRAP_HEIGHT)
class CarouselNoSnap(context: Context) : Carousel(context) {

init {
EpoxyVisibilityTracker().attach(this)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@elihart elihart merged commit c6945f6 into airbnb:master Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants