2018-11-21

CoordinateLayout打造酷炫效果

一脆丁、CoordinatorLayout簡介

CoordinatorLayout是Android5.0之后推出的一個功能,它與AppBarLayout結(jié)合可以打造非撤逄拢酷炫的效果.一般將CoordinatorLayout布局分為兩部分,上半部分是AppBarLayout包裹的內(nèi)容,實現(xiàn)該布局上滑隱藏,下滑顯示的效果;下半部分是可滑動的布局,一般設置為RecyclerView,ListView或者時ViewPager等哈垢。

二赖捌、結(jié)合ToolBar

效果圖:


toolbar.gif

2.1 具體實現(xiàn)

布局文件代碼:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sunny.myframedemo.toolbarscroll.ToolBarFragment">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolBar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways">
            </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/add_2"/>
</android.support.design.widget.CoordinatorLayout>

布局結(jié)構(gòu)為:


toolbar.png

最外層是CoordinatorLayout,AppBarLayout為它的子布局封恰,使用AppBarLayout將ToolBar包裹麻养,最關(guān)鍵的就是給ToolBar指定layout_scrollFlags屬性。

2.2 幾個重要的屬性

2.2.1 Scroll

值設為scroll的View會跟隨滾動事件一起發(fā)生移動诺舔。就是當指定的ScrollView發(fā)生滾動時鳖昌,該View也跟隨一起滾動,就好像這個View也是屬于這個ScrollView一樣低飒。

2.2.2 enterAlways

值設為enterAlways的View,當任何時候ScrollView往下滾動時许昨,該View會直接往下滾動。而不用考慮ScrollView是否在滾動到最頂部還是哪里褥赊。

2.2.3 exitUntilCollapsed

值設為exitUntilCollapsed的View车要,當這個View要往上逐漸“消逝”時,會一直往上滑動崭倘,直到剩下的的高度達到它的最小高度后翼岁,再響應ScrollView的內(nèi)部滑動事件类垫。

2.2.4 enterAlwaysCollapsed

是enterAlways的附加選項,一般跟enterAlways一起使用琅坡,它是指悉患,View在往下“出現(xiàn)”的時候,首先是enterAlways效果榆俺,當View的高度達到最小高度時售躁,View就暫時不去往下滾動,直到ScrollView滑動到頂部不再滑動時茴晋,View再繼續(xù)往下滑動陪捷,直到滑到View的頂部結(jié)束。
剩下的就是給recyclerView填充數(shù)據(jù)了诺擅。

三市袖、結(jié)合ViewPager

效果圖:


viewpager.gif

3.1 具體實現(xiàn)

布局代碼:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sunny.myframedemo.toolbarscroll.AppBarFragment">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/beauty2"
            android:scaleType="fitXY"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="?attr/colorPrimary"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="4dp"
            app:tabSelectedTextColor="#000"
            app:tabTextColor="#fff"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/appbar_viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v4.view.ViewPager>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/add_2" />

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

布局結(jié)構(gòu):


viewpager.png

布局結(jié)構(gòu)與CoordinatorLayout結(jié)合ToolBar類似。這里只要講解以下TabLayout與ViewPager綁定烁涌。

3.2 ViewPager與TabLayout綁定

3.2.1 布局

布局文件上述已經(jīng)給出了苍碟,這里給出幾個TabLayout的幾個屬性:
① app:tabIndicatorColor 表示下劃線顏色
② app:tabIndicatorHeight 表示下方指示條的高度
③ app:tabSelectedTextColor 表示tab被選中后,文字的顏色
④ app:tabTextColor 表示tab中字體的顏色

3.2.2 初始化標題欄與fragment

① 標題欄初始化

    private String[] mtitle = new String[]{
            "微博",
            "發(fā)現(xiàn)",
            "相冊"
    };

② fragment的創(chuàng)建

public class MyItemFragment extends Fragment {


    public static MyItemFragment newInstance(String title){
        MyItemFragment myItemFragment=new MyItemFragment();
        Bundle bundle = new Bundle();
        bundle.putString("Title", title);
        myItemFragment.setArguments(bundle);
        return myItemFragment;
    }

    private RecyclerView recyclerView;
    private String title;
    private List<String>  mList=new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_my_item, container, false);
        recyclerView=view.findViewById(R.id.item_recyclerView);

        Bundle bundle=getArguments();
        if (bundle !=null){
            title=bundle.getString("Title");
        }

        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        initData();

        ToolBarAdapter toolBarAdapter=new ToolBarAdapter(mList);
        recyclerView.setAdapter(toolBarAdapter);

        return view;
    }

    private void initData(){
        for (int i=0;i<20;i++){
            mList.add("我是第"+i+"個"+title);
        }
    }

給fragment的recyclerView中填充數(shù)據(jù)撮执,然后顯示微峰。

3.2.3 為ViewPager創(chuàng)建適配器

public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragmentList;
    private String[] mTitle;

    public MyPagerAdapter(FragmentManager fm) {
        this(fm, null, null);
    }

    public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, String[] mTitle) {
        super(fm);
        if (fragmentList == null) {
            fragmentList = new ArrayList<>();
        }
        this.fragmentList = fragmentList;
        this.mTitle = mTitle;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitle[position];
    }
}

這里需要注意一點,必須重寫getPageTitle方法要不然就標題就不會顯示出來抒钱。

3.2.4 ViewPager與TableLayout綁定

tabLayout.setupWithViewPager(viewPager);

四蜓肆、結(jié)合ViewPager+CollapsingToolbarLayout

效果圖:


collasp.gif

4.1 具體實現(xiàn)

界面布局代碼:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:fitsSystemWindows="true"
    tools:context="com.sunny.myframedemo.toolbarscroll.CollapsingFragment">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:fitsSystemWindows="true"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/beauty3"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/collap_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light" />

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

        <android.support.design.widget.TabLayout
            android:id="@+id/collap_tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="?attr/colorPrimary"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="4dp"
            app:tabSelectedTextColor="#000"
            app:tabTextColor="#fff" />

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

        <android.support.v4.view.ViewPager
            android:id="@+id/collap_viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        </android.support.v4.view.ViewPager>

        <android.support.design.widget.FloatingActionButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="15dp"
            android:src="@drawable/add_2"/>
</android.support.design.widget.CoordinatorLayout>

布局結(jié)構(gòu)如下:


collapse.png

AppBarLayout將CollapsingToolBarLayout和TabLayout包裹起來,而在CollapsingToolBarLayout里面包含ImageView和ToolBar

4.1.1 CollapsingToolBarLayout的3種折疊模式

① off: 這個是默認屬性谋币,布局將正常顯示仗扬,沒有折疊的行為。
②pin:CollapsingToolbarLayout折疊后瑞信,此布局將固定在頂部。
③parallax:CollapsingToolbarLayout折疊時穴豫,此布局也會有視差折疊效果凡简。
剩下的就是將ViewPager與TabLayout綁定了,這里就不贅述了精肃。
最后附上源碼:
https://github.com/sunnyslx/CoordinatorLayoutDemo.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末秤涩,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子司抱,更是在濱河造成了極大的恐慌筐眷,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件习柠,死亡現(xiàn)場離奇詭異匀谣,居然都是意外死亡照棋,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門武翎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來烈炭,“玉大人,你說我怎么就攤上這事宝恶》叮” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵垫毙,是天一觀的道長霹疫。 經(jīng)常有香客問我,道長综芥,這世上最難降的妖魔是什么丽蝎? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮毫痕,結(jié)果婚禮上征峦,老公的妹妹穿的比我還像新娘。我一直安慰自己消请,他們只是感情好栏笆,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著臊泰,像睡著了一般蛉加。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上缸逃,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天针饥,我揣著相機與錄音,去河邊找鬼需频。 笑死丁眼,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的昭殉。 我是一名探鬼主播苞七,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼挪丢!你這毒婦竟也來了蹂风?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤乾蓬,失蹤者是張志新(化名)和其女友劉穎惠啄,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡撵渡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年融柬,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片姥闭。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡丹鸿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出棚品,到底是詐尸還是另有隱情靠欢,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布铜跑,位于F島的核電站门怪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏锅纺。R本人自食惡果不足惜掷空,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望囤锉。 院中可真熱鬧坦弟,春花似錦、人聲如沸官地。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽驱入。三九已至赤炒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間亏较,已是汗流浹背莺褒。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留雪情,地道東北人遵岩。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像巡通,于是被迫代替她去往敵國和親尘执。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

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