Skip to content

Commit

Permalink
Create startLiftAnimation function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandregpereira committed Oct 21, 2020
1 parent b37f089 commit 37276d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 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
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 37276d3

Please sign in to comment.