欢迎使用CSDN-markdown编辑器

来源:互联网 发布:台湾的未来 知乎 编辑:程序博客网 时间:2024/06/06 03:56

代码实现折叠Toolbar主动收起

布局文件

  • 注意layout_behavior属性所在控件是支持包中的NestedScrollView
<android.support.design.widget.CoordinatorLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true">    <android.support.design.widget.AppBarLayout        android:layout_width="match_parent"        android:layout_height="220dp">        <android.support.design.widget.CollapsingToolbarLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:fitsSystemWindows="true"            app:contentScrim="@color/colorPrimary"            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"            app:titleEnabled="false">            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_gravity="center"                android:layout_marginTop="20dp"                android:gravity="center"                android:orientation="vertical"                app:layout_collapseMode="parallax"                app:layout_collapseParallaxMultiplier="0.7">                <ImageView                    android:layout_width="90dp"                    android:layout_height="90dp"                    android:src="@drawable/ic_head_default" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_marginTop="@dimen/D5dp"                    android:textColor="@color/white"                    android:textSize="22sp" />            </LinearLayout>            <android.support.v7.widget.Toolbar                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                android:background="?attr/colorPrimary"            </android.support.v7.widget.Toolbar>          </android.support.design.widget.CollapsingToolbarLayout>    </android.support.design.widget.AppBarLayout>    <android.support.v4.widget.NestedScrollView        android:id="@+id/scrollView"        android:layout_width="match_parent"        android:layout_height="wrap_content"       app:layout_behavior="@string/appbar_scrolling_view_behavior">            <include layout="@layout/act_edit2_content_layout" />    </android.support.v4.widget.NestedScrollView ></android.support.design.widget.CoordinatorLayout>

java代码
- 以下代码参考NestedScrollView中onTouchEvent方法

//拿到layout_behavior属性所在的控件NestedScrollView scrollView = (NestedScrollView) findViewById(R.id.scrollView);//获取状态栏高度Rect rectangle= new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);/*调用NestedScrollView中的startNestedScroll方法(继承自ViewCompat),设置滚动方式*/scrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL); /* 调用NestedScrollView中的dispatchNestedPreScroll方法(继承自View,参数一:X方向移动距离,参数二:Y方向移动距离,参数三四不重要),使NestedScrollView向上移动至顶部,因为NestedScrollView通过layout_behavior与折叠Toolbar关联,所以折叠toolbar会自动向上联动,以达到主动收起折叠Toolbar的目的*/scrollView.dispatchNestedPreScroll(0,(scrollView.getTop() - (toolbar.getHeight() + rectangle.top)),new int[2],new int[2]);
0 0