美文网首页
CoordinatorLayout+自定义Behavior

CoordinatorLayout+自定义Behavior

作者: 谦谦行者 | 来源:发表于2023-01-17 17:23 被阅读0次

布局文件

<?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=".WeiBoDetailActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:textColor="@color/white"
            app:layout_scrollFlags="scroll|enterAlways"
            android:text="我是header"/>
    </com.google.android.material.appbar.AppBarLayout>
    <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/footer"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/black"
        android:layout_gravity="bottom"
        app:layout_behavior="@string/footer_behavior2"
        android:text="我是底部"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

实现效果,header跟随滑动隐藏、显示,footer跟随滑动隐藏(使用官方的behavior)、显示(自定义behavior)

footer_behavior2

class MyFooterBehavior2(context: Context?, attrs: AttributeSet?) :
    CoordinatorLayout.Behavior<View>(context, attrs) {

    var showAnimator : ViewPropertyAnimator? = null
    var isShowAnimEnd = true
    var isHideAnimEnd = true
    override fun onStartNestedScroll(
        coordinatorLayout: CoordinatorLayout,
        child: View,
        directTargetChild: View,
        target: View,
        axes: Int,
        type: Int
    ): Boolean {
        return (axes and ViewCompat.SCROLL_AXIS_VERTICAL) !== 0
    }

    // 根据滑动的距离,显示或隐藏footerView
    // child 就是要改变的那个view
    override fun onNestedScroll(
        coordinatorLayout: CoordinatorLayout,
        child: View,
        target: View,
        dxConsumed: Int,
        dyConsumed: Int,
        dxUnconsumed: Int,
        dyUnconsumed: Int,
        type: Int,
        consumed: IntArray
    ) {
        Log.e("scroll","$dxConsumed;$dyConsumed")
        Log.e("scroll","$dxUnconsumed;$dyUnconsumed")

        if (dyConsumed > 0  && isShowAnimEnd && isHideAnimEnd) { // 上划,显示
            show(child)
        }else if (dyConsumed < 0 && child.visibility == View.VISIBLE && isShowAnimEnd && isHideAnimEnd) { // 下划 隐藏
            hide(child)
        }
    }

    private fun show(view: View) {
        showAnimator = view.animate().translationY(0f)
            .setInterpolator(FastOutSlowInInterpolator())
            .setDuration(200)
        showAnimator?.setListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(p0: Animator?) {
                isShowAnimEnd = false
            }

            override fun onAnimationEnd(p0: Animator?) {
//                view.visibility = View.VISIBLE
                isShowAnimEnd = true
            }

            override fun onAnimationCancel(p0: Animator?) {
                // 中途关闭,应该隐藏
//                view.visibility = View.GONE
//                hide(view)
                isShowAnimEnd = true
            }

            override fun onAnimationRepeat(p0: Animator?) {

            }
        })
        showAnimator?.start()
    }

    private fun hide(view: View) {
        val animator = view.animate().translationY(view.height.toFloat())
            .setInterpolator(FastOutSlowInInterpolator())
            .setDuration(200)
        animator.setListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(p0: Animator?) {
                isHideAnimEnd = false
            }

            override fun onAnimationEnd(p0: Animator?) {
//                view.visibility = View.GONE
                isHideAnimEnd = true
            }

            override fun onAnimationCancel(p0: Animator?) {
                // 中途关闭,应该显示
//                view.visibility = View.VISIBLE
//                show(view)
                isHideAnimEnd = true
            }

            override fun onAnimationRepeat(p0: Animator?) {

            }
        })
        animator.start()
    }
}

相关文章

网友评论

      本文标题:CoordinatorLayout+自定义Behavior

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