CoordinateLayout打造酷炫效果
一脆丁、CoordinatorLayout簡介
CoordinatorLayout是Android5.0之后推出的一個功能,它與AppBarLayout結(jié)合可以打造非撤逄拢酷炫的效果.一般將CoordinatorLayout布局分為兩部分,上半部分是AppBarLayout包裹的內(nèi)容,實現(xiàn)該布局上滑隱藏,下滑顯示的效果;下半部分是可滑動的布局,一般設置為RecyclerView,ListView或者時ViewPager等哈垢。
二赖捌、結(jié)合ToolBar
效果圖:
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)為:
最外層是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
效果圖:
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):
布局結(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
效果圖:
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)如下:
AppBarLayout將CollapsingToolBarLayout和TabLayout包裹起來,而在CollapsingToolBarLayout里面包含ImageView和ToolBar
4.1.1 CollapsingToolBarLayout的3種折疊模式
① off: 這個是默認屬性谋币,布局將正常顯示仗扬,沒有折疊的行為。
②pin:CollapsingToolbarLayout折疊后瑞信,此布局將固定在頂部。
③parallax:CollapsingToolbarLayout折疊時穴豫,此布局也會有視差折疊效果凡简。
剩下的就是將ViewPager與TabLayout綁定了,這里就不贅述了精肃。
最后附上源碼:
https://github.com/sunnyslx/CoordinatorLayoutDemo.git