I have a recycler view below my appbar, and it expands when I'm at the top of my RV and I scroll up one more time. I need my collapsed toolbar to expand when the smooth scrolling animation reaches the top, so I don't need to scroll up again. Instead what I get is that I scroll to the top, and my RV stops, then I have to scroll again just to expand the collapsed toolbar.
I am currently looking into MotionLayout, because in this answer I've read it offers easier behavior customization https://stackoverflow.com/a/55328600/13150066
I don't know how to upload videos here, but if you have an idea and want to check out the behavior I want, check out spotify's playlist.
Is there a solution to my problem so I don't have to change to MotionLayout?
I had to take a look at those MotionLayout and it's super easy. Made it work!
I downloaded Android Studio 4.1 so I could use the new layout design interface and it work wonders. Honestly I thought it was going to be really difficult to achieve, this effect, but it was really simple, and everything is pretty self explainatory. Anyways, I'm leaving you the tutorial I did it with, hope it helps!
https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-1/
I think the most beneficial part of using MotionLayout is that it extends from ConstraintLayout, so your Layouts are laid flat. No Collapsing Toolbar inside an AppBar, with a Toolbar and an ImageView nested. Using MotionLayout, I only used an imageView laid flat and the animations are set in a new XML.
Related
I have created a sample project that can reproduce this issue.
Expected Result
When I scroll the bottom RecyclerView upwards, the top RecyclerView collapses
When I scroll the bottom RecyclerView downwards, the top RecyclerView expands
Observed Result
When I scroll the bottom RecyclerView downwards, the top RecyclerView does not expands
Additional information
Try to hold your swipe when the top RecyclerView collapses at the half, and then swipe down. You can see half of the top items being cut.
This only happens for the collapsing View being/includes a RecyclerView.
GIF
(Ignore the changing color - it is just a result of compressing GIF)
Question
Did I do anything wrong or it is a MotionLayout bug?
I came up with an alternative solution.
For easier communication, I will use rvTop for the top RecyclerView and rvBottom for the bottom RecyclerView.
Instead of "shrinking" rvTop, the idea is to "hide" it instead.
So instead of let rvBottom constraint to the bottom of rvTop, we need to introduce a new invisible line in between them, and use MotionLayout to animate this invisible line upwards, while keeping the alpha change of rvTop.
In order to do this, we have to make rvBottom opaque, otherwise it will overlap with the fading rvTop.
I have updated the code in the sample project to demonstrate the changes.
And the effect is:
EDIT:
Another alternative is to remove rvTop's constraint to top of parent after the transition.
Yes, this will make it simply scroll with the rvBottom; but we can add a little bit of translationY to make it "scroll slower than rvBottom".
This can handle the case where you cannot make rvBottom opaque, for example the page itself uses a gradient background.
The effect looks like this:
My client requested a collapsing view (triggered by a recycler view) that doesn't graphically belong to AppBar / Toolbar abstraction. While I was able to fake it somehow with the mentioned view sitting really inside CollapsingToolbarLayout, I really feel the code is clumsy and will be a nightmare to maintain.
The name CoordinatorLayout suggests that maybe the collapsing / parallax behaviour could be used anywhere in view hierarchy, but I couldn't find neither example nor any proof of in Android docs. All examples show collapsing views only inside AppBars!
So - is it or is it not possible to collapse any view anywhere with events from some RecyclerView?
Since it was requested - a schematic view of the layout. But the question is really more general. As stated above - I've implemented it putting the collapsable square inside AppBar and setting background to white. It works as required, but looks hacky...
I really feel the code is clumsy and will be a nightmare to maintain.
I don't think so.
Also CollapsingToolbarLayout is optional (Remember, you can use minHeight if you want).
But you have to keep AppBarLayout to get things worked. (Don't forget to set app:elevation="0dp" to the AppBarLayout)
Is it or is it not possible to collapse any view anywhere with events
from some RecyclerView?
Yes, it is possible. By attaching an OnScrollListener to the RecyclerView and manually collapsing it. But I think the AppBarLayout method will be enough for this.
I have a vertically scrollable list using a RecyclerView. The layout I'm trying to implement is that when you scroll down far enough and reach a specific item, if you keep scrolling past this item it will stick to the bottom of the screen while the rest of the list continues to scroll behind it. Currently it's implemented by having a scroll listener on the RecyclerView and manually adjusting the position of the sticky view as required, but this is hacky and hard to build on.
Is there an easier way to have this kind of layout? I am currently investigating using a CoordinatorLayout but I'm not sure if it's the right tool for the job.
You can accomplish this using a CoordinatorLayout with a custom behaviour. The behaviour should be applied to the sticky view and make it appear/disappear as the RecyclerView scrolls. You have to override onStartNestedScroll in your behaviour to return true to receive calls for scroll changes.
I've been using Phonograph music player for a while and it's a really good looking app in my opinion. It has a very nice sort of header: basically there's the toolbar wich slides up and down along with the recycler view hiding behind the status bar when scrolling down and coming back visible when scrolling up, the difference with other toolbars show/hide animations is that this one does not actually have two rigid states (hidden or shown) but instead it can be half covered, 70% covered, 80% covered and so on, it moves at the same speed of the recycler view, it's really different than the ActionBar.hide() .
Little clip to explain what I'm talking about:
http://i.imgur.com/JCIiFAA.jpg
I've searched the web for solutions but I haven't found nothing close enough but I think that the Observable Scroll View library might be a good starting point (Have already done some testings but so far it has those 2 rigid states wich I don't want).
I am using an activity MainActivity.java and setting its content view to activity_main.xml, how can I achieve that result?
Also how can I make that view pager selector just below the toolbar but sticky?
CoordinatorLayout from Design Support Library is what you are looking for.
See Tutorial here https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/
First of all you need to wrap up your Layout with Coordinator layout and set this flag on Toolbar
app:layout_scrollFlags="scroll|enterAlways"
I don't know how to describe this very well but I see it in a lot of apps such as Transit.
There is a main window and then a view below it such that when you scroll down, the top window slides up at a slower rate within its own view while the bottom view scrolls up at the same rate the user drags.
Is there a name for this technique?
I believe the effect you are talking about is of Coordinator Layout. You need to implement Coordinator Layout and define view that you want to scroll upward.
One of the example that you can use to learn is Mastering the Coordinator Layout.
Hope this helps. :)