Toolbar-滑动显示
2021-06-27 本文已影响0人
Method
toolbar.gif
原理
利用滑动的距离控制toolbar的alpha,达到上拉显示,下拉隐藏效果
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mRv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/mToolbar"
android:background="@color/purple_200"
app:title="hello"
android:alpha="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
mRv.addOnScrollListener(object :RecyclerView.OnScrollListener(){
//dy 垂直滚动的距离
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
"dy---->$totalDy".e()
//totalDy 总的垂直滑动距离
totalDy += dy
if(totalDy < 0){
return
}
if(totalDy <= 180){
val alpha = totalDy / 180.0f
"alpha---->$alpha".e()
mToolbar.alpha = alpha
}else{
mToolbar.alpha = 1.0f
}
}
})