自定义Behavior,最外层布局必须是CoordinatorLayout
实现的效果,上滑隐藏TextView,下滑显示TextView
Demo链接:https://share.weiyun.com/53UcCFY
效果如下:

public class BottomShowBehavior extends CoordinatorLayout.Behavior<TextView>{
public BottomShowBehavior(Context context, AttributeSet attrs) {
super(context,attrs);
}
// 这个方法的回调时机 : 即将发生嵌套滚动时 nestedScrollAxes 用于判断滑动的方向
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, TextView child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
//发生嵌套滚动的时候 回调
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, TextView child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
//向下滑动
if (dyConsumed + dyUnconsumed > 0) {
//隐藏child
if (child.getVisibility() == View.VISIBLE) {
Anim.bottomHide(child);
}
//向上滑动
} else {
//展示child
if (child.getVisibility() != View.VISIBLE) {
Anim.bottomShow(child);
}
}
}
}
public class Anim {
public static void bottomShow(View show) {
//展示的动画
show.clearAnimation();
Animation animationShow = AnimationUtils.loadAnimation(show.getContext(), R.anim.main_tab_bottom_show);
show.startAnimation(animationShow);
show.setVisibility(View.VISIBLE);
}
public static void bottomHide(View gone) {
//消失的动画
gone.clearAnimation(); //清楚自身动画
Animation animationGone = AnimationUtils.loadAnimation(gone.getContext(), R.anim.main_tab_bottom_hide);
gone.startAnimation(animationGone);
gone.setVisibility(View.INVISIBLE);
}
}
main_tab_bottom_hide.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="100%" />
</set>
main_tab_bottom_show.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
别忘了在string里面设置behavior属性
<resources>
<string name="app_name">BehaviorLibrary</string>
<string name="bottom_show_behavior">com.sloop.behaviorlibrary.design.BottomShowBehavior</string>
</resources>
使用app:layout_behavior="@string/bottom_show_behavior" 设置即可
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<TextView
android:id="@+id/tv_bottom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:background="#f00"
android:gravity="center"
android:text="我是底部"
android:textColor="#fff"
app:layout_behavior="@string/bottom_show_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
网友评论