滑动显示隐藏ToolBar

 <android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:sct="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <android.support.v4.widget.NestedScrollView
        ……/>

    <LinearLayout
        android:id="@+id/llTitle"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dm_75"
        android:background="@android:color/transparent"
        android:orientation="vertical"
        android:paddingTop="@dimen/dm_25">

        <android.support.v7.widget.Toolbar
            android:id="@+id/alphaToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:visibility="gone"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:titleTextColor="@color/white">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:text="@string/home_title"
                android:textColor="@color/white"
                android:textSize="18dp" />
        </android.support.v7.widget.Toolbar>
    </LinearLayout>
  </android.support.design.widget.CoordinatorLayout>

在代码中设置监听事件进行滑动显示隐藏

   mScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            int toolbarHeight = llTitle.getHeight();
            //当滑动的距离 <= toolbar高度的时候,改变Toolbar背景色的透明度,达到渐变的效果
            if (mScroll.getScrollY() <= toolbarHeight) {
                float scale = (float) mScroll.getScrollY() / toolbarHeight;
                float alpha = scale * 255;
                llTitle.setBackgroundColor(Color.argb((int) alpha, 128, 0, 0));
                alphaToolbar.setVisibility(View.GONE);
            } else {
                //上述虽然判断了滑动距离与toolbar高度相等的情况,但是实际测试时发现,标题栏的背景色
                //很少能达到完全不透明的情况,所以这里又判断了滑动距离大于toolbar高度的情况,
                //将标题栏的颜色设置为完全不透明状态
                llTitle.setBackgroundResource(R.color.colorPrimary);
                alphaToolbar.setVisibility(View.VISIBLE);
            }
        }
    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 先来看下效果图 本文使用到RecyclerView、CardView、butterknife、FloatingAc...
    JC_Hou阅读 7,288评论 4 26
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,016评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,465评论 4 61
  • 秋天,叶子的视野,柿子的形态。田野村庄越渐奢侈,从未远离,以为回得去。桂香铺天盖地,菊香另辟蹊径,甜腻苦淡各自入味...
    第九棵香樟树阅读 2,454评论 0 0
  • 什么是最好的书法,目前还没有确切的定义,本人认为,看起来笔法自然流畅或苍劲有力,给人有一种美的享受和舒服感就是好作...
    毛笔书法爱好者阅读 3,875评论 0 2

友情链接更多精彩内容