android安卓定义,安卓自定义Behavior

自定义Behavior,最外层布局必须是CoordinatorLayout

实现的效果,上滑隐藏TextView,下滑显示TextView

效果如下:

147d1138b745

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值