2018-11-21

CoordinateLayout打造酷炫效果

一、CoordinatorLayout簡(jiǎn)介

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

二、結(jié)合ToolBar

效果圖:


toolbar.gif

2.1 具體實(shí)現(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 幾個(gè)重要的屬性

2.2.1 Scroll

值設(shè)為scroll的View會(huì)跟隨滾動(dòng)事件一起發(fā)生移動(dòng)。就是當(dāng)指定的ScrollView發(fā)生滾動(dòng)時(shí),該View也跟隨一起滾動(dòng),就好像這個(gè)View也是屬于這個(gè)ScrollView一樣。

2.2.2 enterAlways

值設(shè)為enterAlways的View,當(dāng)任何時(shí)候ScrollView往下滾動(dòng)時(shí),該View會(huì)直接往下滾動(dòng)。而不用考慮ScrollView是否在滾動(dòng)到最頂部還是哪里。

2.2.3 exitUntilCollapsed

值設(shè)為exitUntilCollapsed的View,當(dāng)這個(gè)View要往上逐漸“消逝”時(shí),會(huì)一直往上滑動(dòng),直到剩下的的高度達(dá)到它的最小高度后,再響應(yīng)ScrollView的內(nèi)部滑動(dòng)事件。

2.2.4 enterAlwaysCollapsed

是enterAlways的附加選項(xiàng),一般跟enterAlways一起使用,它是指,View在往下“出現(xiàn)”的時(shí)候,首先是enterAlways效果,當(dāng)View的高度達(dá)到最小高度時(shí),View就暫時(shí)不去往下滾動(dòng),直到ScrollView滑動(dòng)到頂部不再滑動(dòng)時(shí),View再繼續(xù)往下滑動(dòng),直到滑到View的頂部結(jié)束。
剩下的就是給recyclerView填充數(shù)據(jù)了。

三、結(jié)合ViewPager

效果圖:


viewpager.gif

3.1 具體實(shí)現(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)給出了,這里給出幾個(gè)TabLayout的幾個(gè)屬性:
① app:tabIndicatorColor 表示下劃線顏色
② app:tabIndicatorHeight 表示下方指示條的高度
③ app:tabSelectedTextColor 表示tab被選中后,文字的顏色
④ app:tabTextColor 表示tab中字體的顏色

3.2.2 初始化標(biāo)題欄與fragment

① 標(biāo)題欄初始化

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

② 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+"個(gè)"+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];
    }
}

這里需要注意一點(diǎn),必須重寫getPageTitle方法要不然就標(biāo)題就不會(huì)顯示出來。

3.2.4 ViewPager與TableLayout綁定

tabLayout.setupWithViewPager(viewPager);

四、結(jié)合ViewPager+CollapsingToolbarLayout

效果圖:


collasp.gif

4.1 具體實(shí)現(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: 這個(gè)是默認(rèn)屬性,布局將正常顯示,沒有折疊的行為。
②pin:CollapsingToolbarLayout折疊后,此布局將固定在頂部。
③parallax:CollapsingToolbarLayout折疊時(shí),此布局也會(huì)有視差折疊效果。
剩下的就是將ViewPager與TabLayout綁定了,這里就不贅述了。
最后附上源碼:
https://github.com/sunnyslx/CoordinatorLayoutDemo.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,030評(píng)論 2 59
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,119評(píng)論 25 708
  • CoordinatorLayout與滾動(dòng)的處理 CoordinatorLayout實(shí)現(xiàn)了多種Material De...
    cxm11閱讀 6,814評(píng)論 1 15
  • 本文出自 “阿敏其人” 簡(jiǎn)書博客,轉(zhuǎn)載或引用請(qǐng)注明出處。 本文主要涉及android里面md設(shè)計(jì)的幾個(gè)控件Coor...
    阿敏其人閱讀 10,631評(píng)論 9 43
  • 姓名:尹飛麟 公司:紹興嘉華砂洗廠 組別:利他一組 《日精打卡進(jìn)行中》 《知~學(xué)習(xí)》朗讀 六項(xiàng)精進(jìn)讀一遍寫一遍 大...
    謙虛不要驕傲閱讀 109評(píng)論 0 0

友情鏈接更多精彩內(nèi)容