美文网首页
安卓自定义Behavior

安卓自定义Behavior

作者: it奔跑在路上 | 来源:发表于2020-03-01 21:57 被阅读0次

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

实现的效果,上滑隐藏TextView,下滑显示TextView
Demo链接:https://share.weiyun.com/53UcCFY
效果如下:
GIF.gif
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>

相关文章

  • 安卓自定义Behavior

    自定义Behavior,最外层布局必须是CoordinatorLayout 实现的效果,上滑隐藏TextView,...

  • 学习自定义View的一些文章

    安卓自定义View基础:坐标系 安卓自定义View基础:角度弧度 安卓自定义View基础:颜色 Android自定...

  • 安卓自定义View教程-1

    基础篇 安卓自定义View基础 - 坐标系 安卓自定义View基础 - 角度弧度 安卓自定义View基础 - 颜色...

  • [转]自定义View的学习

    原文连接 如何关闭硬件加速 自定义View 基础篇 安卓自定义View基础 - 坐标系 安卓自定义View基础 -...

  • Android开源库

    UI 之自定义 Behavior 实现 AppBarLayout 越界弹性效果 使用自定义 Behavior 实现...

  • MVP

    UI 之自定义 Behavior 实现 AppBarLayout 越界弹性效果 使用自定义 Behavior 实现...

  • Android-CoordinatorLayout之自定义Beh

    1.自定义Behavior 自定义Behavior可以重写的方法: 1.onInterceptTouchEvent...

  • 自定义View教程目录

    参考安卓自定义View教程目录

  • Dialog

    安卓dialog的使用+如何自定义dialog自定义Dialog自定义Dialog 自定义

  • 安卓画笔setShadowLayer与SetMaskFilter

    安卓自定义 View 踩坑笔记,特作文记录 安卓 Paint 类用于自定义 View 时↑这两个方法能用来干嘛我就...

网友评论

      本文标题:安卓自定义Behavior

      本文链接:https://www.haomeiwen.com/subject/jotlkhtx.html