不知不覺,這已經(jīng)是『Material Design入門學(xué)習(xí)筆記』專題第六篇文章了助析,結(jié)束了這篇文章犀被,這個專題,會暫時告一段落外冀。但不是結(jié)束寡键,也許僅僅是另一個開始。
『Material Design入門學(xué)習(xí)筆記』前言
『Material Design入門學(xué)習(xí)筆記』動畫(含demo)
『Material Design 入門學(xué)習(xí)筆記』主題與 AppCompatActivity(附 demo)
『Material Design入門學(xué)習(xí)筆記』RecyclerView與CardView(附demo)
『Material Design 入門學(xué)習(xí)筆記』CollapsingToolbarLayout 與 AppBarLayout(附 demo)
demo下載
前言
首先要說明雪隧,這篇文章同樣會用到CoordinatorLayout和AppBarLayout西轩,這兩個組件的相關(guān)知識,可以參考上一篇文章:
[『Material Design 入門學(xué)習(xí)筆記』CollapsingToolbarLayout 與 AppBarLayout(附 demo)]
TabLayout
布局文件
與之前的CollapsingToolbarLayout類似:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/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/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#ffffff"
app:tabMode="scrollable"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
具體介紹一下TabLayout中的一些參數(shù):
-
app:tabIndicatorColor="@color/white"
下方滾動條的下劃線顏色 -
app:tabSelectedTextColor="@color/gray"
tab被選中后脑沿,文字的顏色 -
app:tabTextColor="@color/white"
tab默認(rèn)的文字顏色 -
app:tabMode
可以設(shè)置為fixed和scrollable藕畔。當(dāng)設(shè)置為scrollable表示此TabLayout中當(dāng)子view數(shù)量過多,超出屏幕邊界時候庄拇,可以通過滑動見到那些不可見的子view注服,fix則不可以滑動。 -
app:tabGravity
可以設(shè)置為fill和center丛忆。如果TabLayout中的子view數(shù)量較少時祠汇,如果選擇fill是自動充滿,如果選擇center則居中顯示熄诡。
代碼
首先先看一下Activity中的代碼:
private void initViews(){
mTabLayout = (TabLayout)findViewById(R.id.tabs);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
nestedFragment = new NestedScrollFragment();
emptyFragment1 = new FirstEmptyFragment();
emptyFragment2 = new FirstEmptyFragment();
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(nestedFragment);
fragments.add(emptyFragment1);
fragments.add(emptyFragment2);
ArrayList<String> titles = new ArrayList<>();
titles.add("NestedScroll");
titles.add("empty1");
titles.add("empty2");
FragmentsAdapter mAdapter = new FragmentsAdapter(getSupportFragmentManager(),fragments,titles);
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(0)));
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(1)));
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(2)));
mViewPager.setAdapter(mAdapter);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.setupWithViewPager(mViewPager,false);
}
首先viewpager需要一個Adapter可很,這個等下說。然后我們需要一個tab的名字凰浮,這個可以通過mTabLayout.newTab().setText
進(jìn)行設(shè)置,同樣還可以通過mTabLayout.newTab().setIcon()
設(shè)置圖片我抠。然后通過TabLayout的addTab
方法進(jìn)行添加。
然后為TabLayout設(shè)置對應(yīng)的viewpager即可袜茧。
這里給出FragmentsAdapter中的代碼:
public class FragmentsAdapter extends FragmentPagerAdapter {
private List<Fragment> list_fragment;
private List<String> list_Title;
public FragmentsAdapter(FragmentManager fm, List<Fragment> list_fragment, List<String> list_Title) {
super(fm);
this.list_fragment = list_fragment;
this.list_Title = list_Title;
}
@Override
public Fragment getItem(int position) {
return list_fragment.get(position);
}
@Override
public int getCount() {
return list_Title.size();
}
@Override
public CharSequence getPageTitle(int position) {
return list_Title.get(position % list_Title.size());
}
}
對于代碼中提到過的fragment可以自己寫菜拓,也可以參考我的demo,下面給一張圖:
NestedScrollView
關(guān)于NestedScrollView笛厦,我并沒有發(fā)現(xiàn)與ScrollView有什么明顯的差別或優(yōu)勢纳鼎,NestedScrollView的存在是指為了適配MD的其他組件而存在的,RecyclerView代替了ListView裳凸,而NestedScrollView代替了ScrollView贱鄙,他們兩個都可以用來跟ToolBar交互,在CoordinatorLayout中實現(xiàn)折疊滑動等效果姨谷。
使用NestedScrollView
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:src="@drawable/logo"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_marginTop="2dp"
android:id="@+id/nestedscroll"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recycler_nested"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
這次還是用了CollapsingToolbarLayout逗宁,因為這個的折疊效果比較明顯。然后看下代碼:
for (int i = 0 ; i< 200;i++){
list.add("item:"+i);
}
adapter = new CardListAdapter(this,list);
recyclerView = (RecyclerView)findViewById(R.id.recycler_nested);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
這些代碼在前面的例子中都介紹過梦湘,沒見過的可以參考:
『Material Design入門學(xué)習(xí)筆記』RecyclerView與CardView(附demo)
下面看一下樣圖:
不使用NestedScrollView
如果不使用的情況下瞎颗,會是什么樣呢件甥,首先需要修改一下布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:src="@drawable/logo"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recycler_nested"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
去掉了NestedScrollView,并把RecyclerView添加了app:layout_behavior="@string/appbar_scrolling_view_behavior"
哼拔。
這是發(fā)現(xiàn)也沒什么問題引有,跟剛才區(qū)別不大,這是因為RecyclerView自帶了ScrollView倦逐。但是如果放一個線性布局呢?
試一下轿曙,建立一個線性布局,里面添加多個TextView僻孝。布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:src="@drawable/logo"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!--<android.support.v4.widget.NestedScrollView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:fillViewport="true"-->
<!--android:layout_marginTop="2dp"-->
<!--android:id="@+id/nestedscroll"-->
<!--app:layout_behavior="@string/appbar_scrolling_view_behavior">-->
<!--<android.support.v7.widget.RecyclerView-->
<!--android:layout_width="match_parent"-->
<!--android:id="@+id/recycler_nested"-->
<!--app:layout_behavior="@string/appbar_scrolling_view_behavior"-->
<!--android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>-->
<!--</android.support.v4.widget.NestedScrollView>-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test"/>
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
效果如圖:
這是我們發(fā)現(xiàn),向上滑動守谓,由于ScrollView的原因也會滾動穿铆,但是跟AppBarLayout的交互動畫沒有了。只能在AppBarLayout上向上互動斋荞,才能有折疊的效果荞雏。
所以這也就說明了NestedScrollView與ScrollView的區(qū)別,那就是平酿,對于Material Design動畫和組件的適配凤优。
總結(jié)
『Material Design入門學(xué)習(xí)筆記』暫時就寫到這,后面如果發(fā)現(xiàn)好的東西還會進(jìn)行補(bǔ)充蜈彼,但是近期內(nèi)筑辨,就寫到這里了,有疑問的朋友歡迎給我留言指正幸逆,或者關(guān)注我的公眾號留言: