自定义Behavior,最外层布局必须是CoordinatorLayout
实现的效果,上滑隐藏TextView,下滑显示TextView
效果如下:
GIF.gif
public class BottomShowBehavior extends CoordinatorLayout.Behavior{
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
android:duration="300"
android:fromYDelta="0"
android:toYDelta="100%" />
main_tab_bottom_show.xml
android:duration="300"
android:fromYDelta="100%"
android:toYDelta="0" />
别忘了在string里面设置behavior属性
BehaviorLibrary
com.sloop.behaviorlibrary.design.BottomShowBehavior
使用app:layout_behavior="@string/bottom_show_behavior" 设置即可
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">
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
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" />