CollapsingToolbarLayout with fixed/pinned Toolbar and functionality of “enterAlways”

后端 未结 2 1866
小蘑菇
小蘑菇 2021-01-25 08:33

I have a standard \"CollapsingToolbarLayout\" implementation based on material design guidelines.

With the the settings below I was able to achieve behavior depicted on

2条回答
  •  天涯浪人
    2021-01-25 09:15

    The default CollapsingToolbarLayout provided by the Material library is too limited to a set of predefined animations. To create our own custom effects, say you want to fully expand the header layout when scrolling up on the RecyclerView/ NestedScrollView even if you are not on top of the scroll view, you can use the powerful MotionLayout which is a subclass of ConstraintLayout for building animations. If you are happy to replace the existing view hierarchy with a flat Constraint-layout equivalent, read the detailed answer given below.


    Here I am going to show you how to create the "enterAlways" effect with an always pinned header layout, all with in three simple steps.

    Before writing any code, see the gif image given below to better understand what we are trying to create.

    1. Add the ConstraintLayout dependency:

    To use MotionLayout in your project, add the ConstraintLayout 2.0 dependency to your app's build.gradle file. If you're using AndroidX, add the following dependency:

    dependencies {
        implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
    }
    

    If you aren't using AndroidX, add the following support library dependency:

    dependencies {
        implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta2'
    }
    

    2. Create the MotionLayout file:

    MotionLayout is a subclass of ConstraintLayout, so you can transform any existing ConstraintLayout into a MotionLayout. So create one layout file as shown below.

    activity_main.xml

    
    
    
    
    
    
    
    
    
    
        
    
            
    
            
        
    
    
        
    
            
    
            
        
    
        
    
            
    
            
        
    
    
    
    
    
    
    
        
    
        
    
        
    
    
    
    
        
    
    
    
    
    

    3. Create a MotionScene:

    In the previous step, motion:layoutDescription attribute references a MotionScene. A MotionScene is an XML resource file that contains all of the motion descriptions for the corresponding layout. To keep layout information separate from motion descriptions, each MotionLayout references a separate MotionScene. Note that definitions in the MotionScene take precedence over any similar definitions in the MotionLayout.

    Following is a sample MotionScene file that creates the required fixed/pinned toolbar with 'enterAlways' effect:

    Put the file in the xml folder under the res directory.

    motionscene.xml

    
    
    
    
    
        
    
    
    
    
        
        
        
            
        
    
    
    
        
    
        
        
            
        
    
        
    
        
    
    
    
    
    
    

    That's all. You created an amazing custom animation with out writing any java/kotlin code. MotionLayout is fully declarative, meaning that you can describe any transitions in XML, no matter how complex.


    The following repo by Google includes more samples.

    https://github.com/googlesamples/android-ConstraintLayoutExamples

提交回复
热议问题