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

[AppBarLayout] Use a uniform way to determine the target scrolling view #3926

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pubiqq
Copy link
Contributor

@pubiqq pubiqq commented Dec 15, 2023

The view that the AppBarLayout should use to determine whether it should be lifted is set using the setLiftOnScrollTargetView or setLiftOnScrollTargetViewId methods. If the target view is not explicitly set, the AppBarLayout should use a closest scrollable view inside CoordinatorLayout's child with appbar_scrolling_view_behavior (due to the CoordinatorLayout pattern).

Unfortunately, the target view is not determined in this way everywhere, so in some cases we may observe incorrect widget state. This PR fixes this issue and unifies the way the target scrolling view is determined.

Fixes #2591, fixes #3925, fixes #4184.

@dsn5ft
Copy link
Contributor

dsn5ft commented Dec 18, 2023

note that after applying this PR, AppBarLayout in NavigationRailDemoControlsFragment will not be lifted automatically because the nested NestedScrollView doesn't match the condition above

This seems like a change/break in the behavior. I think it's reasonable to expect the developer to provide the liftOnScrollTargetViewId in cases where there is a nested scrolling view (c695802).

@dsn5ft
Copy link
Contributor

dsn5ft commented Dec 18, 2023

Also, in some cases I believe the doubly nested scrolling works without flickering via the standard CoordinatorLayout APIs (and no liftOnScrollTargetViewId), so this PR could be disabling the current lift on scroll effect for those cases if I understand correctly.

@pubiqq
Copy link
Contributor Author

pubiqq commented Dec 18, 2023

This seems like a change/break in the behavior. I think it's reasonable to expect the developer to provide the liftOnScrollTargetViewId in cases where there is a nested scrolling view (c695802).

With that comment I just wanted to say that this PR fixes only the flickering issue, but does not lead to the expected lifting of AppBarLayout. Of course, the PR doesn't break the behavior of the component if the liftOnScrollTargetViewId is set.

@pubiqq
Copy link
Contributor Author

pubiqq commented Dec 18, 2023

Also, in some cases I believe the doubly nested scrolling works without flickering via the standard CoordinatorLayout APIs (and no liftOnScrollTargetViewId), so this PR could be disabling the current lift on scroll effect for those cases if I understand correctly.

Well, yes, but this is more of an accident than an intended behavior. According to the javadocs, this should only work on AppBarLayout siblings, not on any nested views:

* <p>AppBarLayout also requires a separate scrolling sibling in order to know when to scroll. The
* binding is done through the {@link ScrollingViewBehavior} behavior class, meaning that you should
* set your scrolling view's behavior to be an instance of {@link ScrollingViewBehavior}. A string

* it should instead be manually updated via {@link #setLifted(boolean)}. If false, the {@link
* AppBarLayout} will manage its lifted state based on the scrolling sibling view.

* <p>If set to true, the {@link AppBarLayout} will animate to the lifted, or elevated, state when
* content is scrolled beneath it. Requires
* `app:layout_behavior="@string/appbar_scrolling_view_behavior` to be set on the scrolling
* sibling (e.g., `NestedScrollView`, `RecyclerView`, etc.).

@pubiqq pubiqq force-pushed the appbar/find-target-scroll-view branch from bc46fe8 to ced300d Compare December 18, 2023 19:51
@dsn5ft
Copy link
Contributor

dsn5ft commented Dec 18, 2023

The ScrollingViewBehavior itself should definitely be set on the sibling, but technically CoordinatorLayout supports sending up nested scrolling events up the hierarchy via the behavior's APIs.

@pubiqq
Copy link
Contributor Author

pubiqq commented Dec 18, 2023

The ScrollingViewBehavior itself should definitely be set on the sibling, but technically CoordinatorLayout supports sending up nested scrolling events up the hierarchy via the behavior's APIs.

It is, but I don't know what you're getting at. setLiftOnScrollTargetView()/setLiftOnScrollTargetViewId() works for any CoordinatorLayout descendant. appbar_scrolling_view_behavior works (and should work) only for CoordinatorLayout children due to the Coordinator pattern + docs.

@dsn5ft
Copy link
Contributor

dsn5ft commented Dec 20, 2023

I'm trying to understand whether this will cause a regression for some cases where appbar_scrolling_view_behavior is set and there is a nested scrolling view (e.g. FrameLayout with appbar_scrolling_view_behavior and NestedScrollView child), but liftOnScrollTargetViewId is not set. Since I believe in some cases that setup works well without flickering. I can also patch the PR and test a few different cases.

@pubiqq
Copy link
Contributor Author

pubiqq commented Dec 20, 2023

I'm trying to understand whether this will cause a regression for some cases where appbar_scrolling_view_behavior is set and there is a nested scrolling view (e.g. FrameLayout with appbar_scrolling_view_behavior and NestedScrollView child), but liftOnScrollTargetViewId is not set. Since I believe in some cases that setup works well without flickering. I can also patch the PR and test a few different cases.

In the current implementation you get undefined behavior. Sometimes AppBarLayout will be lifted, sometimes it won't.

After applying the PR, AppBarLayout will never be lifted, because the direct child with appbar_scrolling_view_behavior (FrameLayout) cannot be scrolled vertically (as intended).

--

I think I've started to understand what you're trying to accomplish, but I don't think that's how it's supposed to work, as it goes against the purpose of CoordinatorLayout.Behavior and also doesn't match the documentation.

@dsn5ft
Copy link
Contributor

dsn5ft commented Dec 20, 2023

After applying the PR, AppBarLayout will never be lifted, because the direct child with appbar_scrolling_view_behavior (FrameLayout) cannot be scrolled vertically (as intended).

A FrameLayout with appbar_scrolling_view_behavior and a scrolling view inside of it can be scrolled vertically, or at least the "scroll events" get passed up through the CoordinatorLayout Behavior APIs (I believe this is intended by the CoordinatorLayout / nested scrolling architecture).

Regardless, I believe we have many pre-existing usages set up that way and it's somewhat common knowledge that appbar_scrolling_view_behavior can be placed on a wrapper <FrameLayout/<Fragment (https://stackoverflow.com/questions/59429919/why-does-my-recyclerview-not-scroll-when-placed-inside-an-appbarlayout).

@pubiqq
Copy link
Contributor Author

pubiqq commented Dec 21, 2023

Well, it seems the words of a random dude on stackoverflow mean more than the documentation, so I have no choice but to add the desired behavior 😂.

@pubiqq pubiqq force-pushed the appbar/find-target-scroll-view branch from ced300d to b3f08c2 Compare December 21, 2023 18:07
@dsn5ft
Copy link
Contributor

dsn5ft commented Jan 8, 2024

I just linked that one post as an example of the pre-existing behavior which is somewhat like a contract at this point. I am aware of many internal and external usages that follow that pattern.

@pubiqq pubiqq force-pushed the appbar/find-target-scroll-view branch from b3f08c2 to fba8a74 Compare January 8, 2024 17:33
@pubiqq
Copy link
Contributor Author

pubiqq commented Jan 8, 2024

Yes, I got it so I added that behavior in fba8a74.

Just in case, I rebased the branch again (b3f08c2) and updated the description.

@pubiqq pubiqq force-pushed the appbar/find-target-scroll-view branch from fba8a74 to 3b13e7c Compare April 11, 2024 21:09
@pubiqq pubiqq mentioned this pull request May 6, 2024
@pubiqq pubiqq force-pushed the appbar/find-target-scroll-view branch from 3b13e7c to 04b2bd0 Compare May 6, 2024 21:07
@pubiqq pubiqq force-pushed the appbar/find-target-scroll-view branch from 04b2bd0 to 788a269 Compare May 21, 2024 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants