Skip to content

Commit

Permalink
Merge pull request #5 from alexandregpereira/dev
Browse files Browse the repository at this point in the history
 Create startLiftAnimation function
  • Loading branch information
alexandregpereira authored Oct 21, 2020
2 parents 8cce50b + 37276d3 commit 08c3a34
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}
}
}
componentsRecycler.setupLiftViewOnScrollCompat(appBarLayout, 2f.dpToPx(resources))
componentsRecycler.setupLiftViewOnScrollCompat(toolbar, 2f.dpToPx(resources))
}
}

Expand Down
28 changes: 11 additions & 17 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@null"
app:title="@string/app_name"
android:layout_margin="0dp"
app:titleTextAppearance="@style/Title.XLarge" />
</com.google.android.material.appbar.AppBarLayout>

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" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/componentsRecycler"
Expand All @@ -34,4 +28,4 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 08c3a34

Please sign in to comment.