android其实很简单--双层Toolbar上拉隐藏实现

本文详细介绍了如何在Android中实现双层Toolbar在上拉时隐藏的效果。首先利用AppBarLayout的layout_scrollFlags和layout_behavior属性配合完成第一层Toolbar的隐藏。接着,通过设置ListView的OnScrollListener并在达到特定高度时,运用属性动画隐藏第二层Toolbar。需要注意ListView的paddingTop设置以及滚动监听的实现,以确保平滑过渡。完整代码示例可在GitHub的DoubleToolbarHideAnimation项目中查看。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现效果

demo

实现过程

第一层Toolbar的隐藏

使用的是AppBarLayout的layout_scrollFlags属性及layout_behavior属性的配合

    // ---> activity_main.xml
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        app:layout_scrollFlags="scroll|enterAlways">
            ...
        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

第二层Toolbar的隐藏

通过OnScrollListener来实现,上拉到一定高度,给第二层Toolbar添加属性动画隐藏掉

这个有几个要注意到地方
* 第二层Toolbar是叠在listiew上的,listview通过paddingTop留下空间,这里需要知道高度
* 设置android:clipToPadding="false",上拉时listview才会覆盖padding的空间

    // ---> fragment_content.xml
    <android.support.v7.widget.RecyclerView
        android:id="@+id/DataListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingTop="@dimen/second_navigation_bar_height"
        android:scrollbars="none" />

然后是滚动监听,(这部分代码是网上,出处忘记了,不过大体都是这样实现的)

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        //移动总距离大于规定距离 并且是显示状态就隐藏
        if (scrolledDistance > hideThreshold && controlsVisible) {
            onHide();
            controlsVisible = false;
            scrolledDistance = 0;//归零
        } else if (scrolledDistance < -hideThreshold && !controlsVisible) {
            onShow();
            controlsVisible = true;
            scrolledDistance = 0;
        }
        //显示状态向上滑动 或 隐藏状态向下滑动 总距离增加
        if ((controlsVisible && dy > 0) || (!controlsVisible && dy < 0)) { 
            scrolledDistance += dy;
        }
    }

然后是属性动画的实现,这里的secondNavigationBarHeight是通过geViewTreeObserver来获取的,防止view未创建时滑动,引起奔溃,正常情况下不会发生,直接使用@dimen/second_navigation_bar_height也可以

    private void hideSeconNavigationBar() {
        if (secondNavigationBarHeight == 0) {
            return;
        }
        ObjectAnimator animator = ObjectAnimator.ofFloat(secondNavigationBar, 
            View.TRANSLATION_Y, 0, -secondNavigationBarHeight);
        animator.setDuration(ANIMATOR_SPEED);
        animator.start();
    }

    private void showSeconNavigationBar() {
        if (secondNavigationBarHeight == 0) {
            return;
        }
        ObjectAnimator animator = ObjectAnimator.ofFloat(secondNavigationBar, 
            View.TRANSLATION_Y, -secondNavigationBarHeight, 0);
        animator.setDuration(ANIMATOR_SPEED);
        animator.start();
    }

demo@github:DoubleToolbarHideAnimation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值