From b37f089348f91a649613f40e91c56a2dea227fd1 Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Wed, 21 Oct 2020 15:12:40 -0300 Subject: [PATCH 1/2] Create startLiftAnimation function --- app/src/main/res/layout/activity_main.xml | 28 +++++++++-------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d25512f..36e4a6c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,28 +1,22 @@ - - - - - - + android:layout_margin="0dp" + android:background="@android:color/white" + app:layout_constraintTop_toTopOf="parent" + app:navigationIcon="@null" + app:title="@string/app_name" + app:titleTextAppearance="@style/Title.XLarge" /> - \ No newline at end of file + \ No newline at end of file From 37276d36579aaf657b40d22d102ae0c5c29d50cf Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Wed, 21 Oct 2020 15:12:58 -0300 Subject: [PATCH 2/2] Create startLiftAnimation function --- .../jerry/app/MainActivity.kt | 2 +- .../animation/ElevationSpringAnimation.kt | 39 +++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt index e035a4e..5a09e06 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt @@ -67,7 +67,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { } } } - componentsRecycler.setupLiftViewOnScrollCompat(appBarLayout, 2f.dpToPx(resources)) + componentsRecycler.setupLiftViewOnScrollCompat(toolbar, 2f.dpToPx(resources)) } } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt index a163f6f..7c2c960 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt @@ -39,16 +39,41 @@ fun View.elevationSpring( targetValue = targetValue ) +@RequiresApi(21) +fun RecyclerView.startLiftAnimation(liftView: View, elevation: Float) { + val layoutManager = this.layoutManager as? LinearLayoutManager ?: return + val adapter = this.adapter + val targetValue: Float = if ( + adapter != null + && adapter.itemCount > 0 + && layoutManager.findFirstCompletelyVisibleItemPosition() > 0 + ) { + elevation + } else { + 0f + } + + if (liftView.elevation == targetValue) { + return + } + + val animation = liftView.elevationSpring(targetValue = targetValue) + if (animation.targetValue == targetValue && animation.isRunning.not()) { + animation.start() + } +} + +fun RecyclerView.startLiftAnimationCompat(liftView: View, elevation: Float) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { + this.startLiftAnimation(liftView, elevation) + } +} + @RequiresApi(21) fun RecyclerView.setupLiftViewOnScroll(liftView: View, elevation: Float) { - val layoutManager = layoutManager as? LinearLayoutManager ?: return + if (layoutManager !is LinearLayoutManager) return addOnScrolled { - val animation = if (layoutManager.findFirstCompletelyVisibleItemPosition() != 0) { - liftView.elevationSpring(targetValue = elevation) - } else { - liftView.elevationSpring(targetValue = 0f) - } - animation.start() + this.startLiftAnimation(liftView, elevation) } }