折叠Toobar

<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.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"

        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"   让这个View可折叠

            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content
可折叠部分
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
      
          不可折叠部分

          
    </LinearLayout>
</android.support.design.widget.AppBarLayout>
 <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

</android.support.design.widget.CoordinatorLayout>

详解

<!--CoordinatorLayout作为布局的协调者,需要使用它,才能完成下面的折叠效果-->
<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:background="#ffffff"
    android:fitsSystemWindows="true">
    <!-- android:fitsSystemWindows="true"
        表示整个布局展示在屏幕时的可用空间是除去了actionbar,title和底部虚拟按键后的剩余区域-->

    <!--AppBarLayout的好处是可以将其内部包含的所有子控件都当做一个整体.作为一个活动条
        AppBarLayout里面包裹CollapsingToolbarLayout和TabLayout.-->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <!--CollapsingToolbarLayout(折叠工具条的布局),作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,
            下面将它包裹了ImageView和Toolbar作为伸缩的区域.通过layout_collapseMode属性来指定其内部的子控件谁可以折叠,
            谁可以固定在屏幕上方.    属性说明:
             1.layout_scrollFlags,它可以控制包含在CollapsingToolbarLayout中的控件(如:ImageView、Toolbar)
                    在响应layout_behavior事件时作出相应的scrollFlags滚动事件(移除屏幕或固定在屏幕顶端)。
               layout_scrollFlags有如下几种选项:
                    scroll: 所有想滚动出屏幕的view都需要设置这个flag-没有设置这个flag的view将被固定在屏幕顶部。
                    enterAlways: 这个flag让任意向下的滚动都会导致该view变为可见,适用于快速“返回模式”。
                    enterAlwaysCollapsed: 当你的视图已经设置minHeight属性又使用此标志时,
                            你的视图只能以最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度。
                    exitUntilCollapsed: 滚动退出屏幕,最后折叠在顶端。
             2.app:expandedTitleMarginStart="10dp",设置扩张时候(还没有收缩时)title离屏幕左边的距离
             3.app:contentScrim="?attr/colorPrimary",设置当完全CollapsingToolbarLayout折叠(收缩)后的背景颜色
        -->








        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="10dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <!-- layout_collapseMode(折叠模式)-有两个值:
                1.parallax:在内容滚动时,CollapsingToolbarLayout中的View(比如ImageView)也可以同时滚动,
                  实现视差滚动效果,通常和layout_collapseParallaxMultiplier(设置视差因子)搭配使用。
                2.pin - 当CollapsingToolbarLayout完全收缩后,Toolbar还可以固定在屏幕上。
            -->
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/title_bg"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"
                />
            <!--标题头-->
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:title="Toolbar">
            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>
        <!--选项卡-->
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextColor="@android:color/black"/>

    </android.support.design.widget.AppBarLayout>

    <!--由于可折叠的这个布局不能直接支持ViewPager的搭配,所以我们可以使用NestedScrollView来解决,
        它是support-v4包提供的控件,继承至FrameLayout,
        并实现了NestedScrollingParent,NestedScrollingChild, ScrollingView接口.
        它的作用类似于Android.widget.ScrollView,不同点在于NestedScrollView支持嵌套滑动-->
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

</android.support.design.widget.CoordinatorLayout>

当android.support.v4.widget.NestedScrollView  中嵌套lListViewshi 时  会出现只显示一条,

或显示不全,只是冲突了可以

<ListView
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:fillViewport="true"
            android:id="@+id/near_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

就不会出现上一种情况  但是又出现了新情况     当ListView条目到达第一条时向下拉无法拉出被折叠部分    只有拉动Toobar

才能拉出被隐藏部分      这个是List  与NestedScrollView冲突导致   recycleView则没有这种问题

当android.support.v4.widget.NestedScrollView   中嵌套Recycle 时    

RecyclerView.setNestedScrollingEnabled(false)

 可以解决滑动卡顿   用RecycleView不会出现ListView  只显示一条  或显示不全

RecyclevIist也可以用下面这种

<android.support.v7.widget.RecyclerView
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:fillViewport="true"
            android:id="@+id/near_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

  

 动态设置SwipeRefreshLayout的是否可以刷新 setEnable(boolean isEnable);//可不
 设置SwipRefreshLayout刷新图标的位置 setProgressViewOffset(true, -20, 100);//可以不

监听 AppBarLayout Offset 变化,动态设置 SwipeRefreshLayout 是否可用

appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset >= 0) {
                    mSwipeRefreshLayout.setEnabled(true);
                } else {
                    mSwipeRefreshLayout.setEnabled(false);
                }
            }
        });

猜你喜欢

转载自blog.csdn.net/weixin_42376563/article/details/81195175