有一個(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è)是有效果的