android CoordinatorLayout器赞、CollapsingToolbarLayout折疊控件基本使用

有一個(gè)折疊布局的需求垢袱,之前有做過,不過當(dāng)時(shí)沒有做技術(shù)整理港柜,今天又用到了请契,還是做下整理省得以后再用到。

需求是這樣的夏醉,上半部分是一個(gè)顯示信息爽锥,下半部分是一個(gè)RecycleView【這里需要做成Viewpager形式的,需要tab切換畔柔,主要是列表】
當(dāng)上滑的時(shí)候氯夷,需要把新華網(wǎng)這個(gè)給隱藏掉,下拉的時(shí)候再顯示出來靶擦,這個(gè)剛好用到了CoordinatorLayout和可折疊的CollapsingToolbarLayout腮考,因?yàn)橄旅嬗辛斜恚砸玫紺oordinatorLayout玄捕,只使用CollapsingToolbarLayout沒有效果

需求.png

代碼實(shí)現(xiàn):主要是在xml中寫就行踩蔚,這個(gè)簡(jiǎn)單的折疊效果,不需要在java代碼中寫枚粘,當(dāng)然復(fù)雜的效果需要些

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <!--標(biāo)題-->
    <include layout="@layout/layout_title"/>

    <!--折疊部分-->
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

**CollapsingToolbarLayout必須和AppBarLayout結(jié)合使用馅闽,而且必須是AppBarLayout的子類**
** 必須設(shè)置app:layout_scrollFlags屬性***
        <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:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white_f8"
                    android:paddingTop="20dp"
                    android:visibility="visible">

                    <LinearLayout
                        android:id="@+id/ll_home_story_number_icon"
                        android:layout_width="64dp"
                        android:layout_height="64dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/shape_circle_history_number_gray"
                        android:gravity="center"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        >

                        <com.chinaso.stories.view.CircularImage
                            android:id="@+id/iv_home_story_number_icon"
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:scaleType="centerCrop"
                            android:src="@mipmap/banner_default"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:gravity="center_vertical"
                        android:orientation="vertical"
                        app:layout_constraintBottom_toBottomOf="@+id/ll_home_story_number_icon"
                        app:layout_constraintLeft_toRightOf="@+id/ll_home_story_number_icon"
                        app:layout_constraintTop_toTopOf="parent"
                        >

                        <com.chinaso.stories.view.BoldTextView
                            android:id="@+id/tv_home_story_number_title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:text="新華社"
                            android:textColor="@color/black_2c"
                            android:textSize="20sp"
                            />

                        <com.chinaso.stories.view.BoldTextView
                            android:id="@+id/tv_home_story_number_from"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp"
                            android:maxLines="1"
                            android:text=" 通訊社"
                            android:textColor="@color/gray_9093"
                            android:textSize="12sp"
                            />
                    </LinearLayout>

                    <com.chinaso.stories.view.BoldTextView
                        android:id="@+id/tv_home_story_number_attention"
                        android:layout_width="65dp"
                        android:layout_height="wrap_content"
                        android:background="@drawable/shape_rectangle_attention_coffee_radius12"
                        android:gravity="center_horizontal"
                        android:paddingLeft="10dp"
                        android:paddingTop="5dp"
                        android:paddingRight="10dp"
                        android:paddingBottom="5dp"
                        android:text="已關(guān)注"
                        android:textColor="@color/white"
                        android:textSize="14sp"
                        android:visibility="gone"
                        app:layout_constraintBottom_toBottomOf="@+id/ll_home_story_number_icon"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        />

                    <com.chinaso.stories.view.BoldTextView
                        android:id="@+id/tv_home_story_number_desc"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="15dp"
                        android:background="@color/white_f8"
                        android:ellipsize="end"
                        android:maxLines="5"
                        android:paddingLeft="25dp"
                        android:paddingTop="12dp"
                        android:paddingRight="25dp"
                        android:paddingBottom="15dp"
                        android:text="新華通訊社,簡(jiǎn)稱新華社馍迄,是中國的國家通訊社福也,法定新聞監(jiān)管機(jī)構(gòu),同時(shí)也是世界性現(xiàn)代通訊社攀圈。新華社是中國共產(chǎn)黨早期創(chuàng)建的重要宣傳輿論機(jī)構(gòu)"
                        android:textColor="@color/gray_60"
                        android:textSize="14sp"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/ll_home_story_number_icon"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/white_d3"
                        app:layout_constraintBottom_toBottomOf="parent"
                        />
                </android.support.constraint.ConstraintLayout>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>


        <!--第二部分-->
**第二部分拟杉,據(jù)我的經(jīng)驗(yàn),好像是只能是一個(gè)布局量承,而這個(gè)布局,沒有規(guī)定必須是哪一個(gè),但是必須要有這個(gè)屬性app:layout_behavior="@string/appbar_scrolling_view_behavior"**
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@color/white"
                android:gravity="center"
                android:text="折疊后撕捍,顯示的控件"
                android:visibility="visible"
                />
            <FrameLayout
                android:id="@+id/frame_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:app="http://schemas.android.com/apk/res-auto"
                                                                   android:id="@+id/refreshLayout"
                                                                   android:layout_width="match_parent"
                                                                   android:layout_height="match_parent"
                                                                   app:srlAccentColor="#00000000"
                                                                   app:srlEnablePreviewInEditMode="true"
                                                                   app:srlPrimaryColor="#00000000">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/rl_story_number_detail"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:paddingLeft="@dimen/dp_10"
                        android:paddingRight="@dimen/dp_10"/>
                </com.scwang.smartrefresh.layout.SmartRefreshLayout>
            </FrameLayout>
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

代碼結(jié)構(gòu)大致如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<!--第一部分拿穴,折疊部分-->
    <android.support.design.widget.CoordinatorLayout
        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:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">


                <需要折疊的View,只能有一個(gè)子View>

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

        <!--第二部分忧风,折疊之后顯示的部分-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        </LinearLayout>
   </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

主要有幾個(gè)屬性默色,需要了解:app:layout_scrollFlags、app:layout_behavior這個(gè)先寫到這狮腿,先去開發(fā)了腿宰,回過頭來再完善這個(gè)內(nèi)容,以上代碼親測(cè)是有效果的

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末缘厢,一起剝皮案震驚了整個(gè)濱河市吃度,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌贴硫,老刑警劉巖椿每,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異英遭,居然都是意外死亡间护,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門挖诸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來汁尺,“玉大人,你說我怎么就攤上這事多律〕胀唬” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵菱涤,是天一觀的道長(zhǎng)苞也。 經(jīng)常有香客問我,道長(zhǎng)粘秆,這世上最難降的妖魔是什么如迟? 我笑而不...
    開封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮攻走,結(jié)果婚禮上殷勘,老公的妹妹穿的比我還像新娘。我一直安慰自己昔搂,他們只是感情好玲销,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著摘符,像睡著了一般贤斜。 火紅的嫁衣襯著肌膚如雪策吠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天瘩绒,我揣著相機(jī)與錄音猴抹,去河邊找鬼。 笑死锁荔,一個(gè)胖子當(dāng)著我的面吹牛蟀给,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播阳堕,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼跋理,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼屯断!你這毒婦竟也來了力奋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤酥艳,失蹤者是張志新(化名)和其女友劉穎越驻,沒想到半個(gè)月后汁政,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡缀旁,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年记劈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片并巍。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡目木,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出懊渡,到底是詐尸還是另有隱情刽射,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布剃执,位于F島的核電站誓禁,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏肾档。R本人自食惡果不足惜摹恰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望怒见。 院中可真熱鬧俗慈,春花似錦、人聲如沸遣耍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽舵变。三九已至酣溃,卻和暖如春瘦穆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背赊豌。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來泰國打工难审, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人亿絮。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像麸拄,于是被迫代替她去往敵國和親派昧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容