CoordinatorLayout,AppBarLayout, NestedScrollView

CoordinatorLayout,AppBarLayout, NestedScrollView,CollapsingToolbarLayout
使用

CoordinatorLayout(使協調)(一般控制子View用layout_gravity的屬性)的使用核心是BehaviorBehavior就是執行你定製的動作。在講Behavior之前必須先理解兩個概念:ChildDependencyChildCoordinatorLayout的子View;其實Child是指要執行動作的CoordinatorLayout的子View。而Dependency是指Child依賴的View就是如過Dependency這個View發生了變化,那麼Child這個View就要相應發生變化。發生變化是具體發生什麼變化呢?這裏就要引入BehaviorChild發生變化的具體執行的代碼都是放在Behavior這個類裏面。(Behaviorz)

AppBarLayout繼承自LinearLayout,佈局方向爲垂直方向。所以你可以把它當成垂直佈局的LinearLayout來使用。AppBarLayout是在LinearLayou上加了一些材料設計的概念,它可以讓你定製當某個可滾動View的滾動手勢發生變化時,其內部的子View實現何種動作。

ActionBar是固定在頂部,並不能移動,我覺得這是最大的不好,而我們的ToolBar可以讓我們隨便擺放,就就可以帶來很多靈活性和效果啦!

正如你所看的這樣,Toolbar根本就不夠看的,一點都不復雜。接下來我們繼續學習在Toolbar上面再套一層父View,讓Toolbar更有互動性。

NestedScrollView相當於scrollView

最少這幾個得在一起用  CoordinatorLayout,AppBarLayout, NestedScrollView

demo1:

<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//主要是用來包裝toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:scaleType="centerCrop"
            android:src="@drawable/ab"
            app:layout_scrollFlags="scroll"//相互協同的屬性  設置爲scroll
            />
會把此佈局當作nestedScrollView的一部分,隨着nestedScrollview的滾動而滾動

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"//這個可以自己改
            app:layout_scrollFlags="scroll|enterAlways" />
設置爲enterAlways會在nestedScrollView向下滑動的時候搶佔向下滑動的觸摸事件,
自己向下滑動等到自己全部顯示時,再把觸摸事件還給nestedScrollView
它纔會向下滑動,而向上滑動不受影響
    </android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
       ..........................................內容....................
 </android.support.v4.widget.NestedScrollView>

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

app:layout_scrollFlags=" exitUntilCollapsed":值設爲exitUntilCollapsed的View,當這個View要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達到它的最小高度後,再響應ScrollView的內部滑動事件。

3 CollapsingToolbarLayout

CollapsingToolbarLayout是用來對Toolbar進行再次包裝的ViewGroup,主要是用於實現摺疊(其實就是看起來像伸縮~)的App Bar效果。它需要放在AppBarLayout佈局裏面,並且作爲AppBarLayout的直接子ViewCollapsingToolbarLayout主要包括幾個功能

(1) 摺疊Title(Collapsing title):當佈局內容全部顯示出來時,title是最大的,但是隨着View逐步移出屏幕頂部,title變得越來越小。你可以通過調用setTitle函數來設置title。

(2)內容紗布(Content scrim):根據滾動的位置是否到達一個閥值,來決定是否對View“蓋上紗布”。可以通過setContentScrim(Drawable)來設置紗布的圖片.

(3)狀態欄紗布(Status bar scrim):根據滾動位置是否到達一個閥值決定是否對狀態欄“蓋上紗布”,你可以通過setStatusBarScrim(Drawable)來設置紗布圖片,但是只能在LOLLIPOP設備上面有作用。


(4)視差滾動子View(Parallax scrolling children):子View可以選擇在當前的佈局當時是否以“視差”的方式來跟隨滾動。(PS:其實就是讓這個View的滾動的速度比其他正常滾動的View速度稍微慢一點)。將佈局參數app:layout_collapseMode設爲parallax
(5)將子View位置固定(Pinned position children):子View可以選擇是否在全局空間上固定位置,這對於Toolbar來說非常有用,因爲當佈局在移動時,可以將Toolbar固定位置而不受移動的影響。 將app:layout_collapseMode設爲pin

<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:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
exitUntilCollapsed:值設爲exitUntilCollapsed的View,當這個View要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達到它的最小高度後,再響應ScrollView的內部滑動事件。
            <ImageView
                android:id="@+id/main.backdrop"
                android:layout_width="wrap_content"
                android:layout_height="300dp"
                android:scaleType="centerCrop"
                android:src="@drawable/ab"
                app:layout_collapseMode="parallax" />
parallx 其實就是讓這個View的滾動的速度比其他正常滾動的View速度稍微慢一點
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                app:layout_collapseMode="pin"  />
pin:子View可以選擇是否在全局空間上固定位置,這對於Toolbar來說非常有用,因爲當佈局在移動時,可以將Toolbar固定位置而不受移動的影響
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView//裏面只能有一個子child

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="50dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
  </android.support.v4.widget.NestedScrollView>

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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章