Starting from the beginning, google's new library, Design Library
, brings several components ready with some common Material Design interactions.
Just include the library as a dependency on your build.gradle
:
compile 'com.android.support:design:23.1.0'
# Outras dependencias implicitas...
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:recyclerview-v7:23.1.0'
Only here comes the surprise ... Along with Design Library
comes other 3 that it also depends ... If you already use them, no problem. But you can overhead your app if you do not use them.
In addition, to implement the indicators I recommend using the ViewPagerIndicator
of Jake Wharton.
With this, the dependency has to be included as well:
compile 'com.viewpagerindicator:library:2.4.1@aar'
But as JakeWharton did not publish his library as an AAR, you need to include the following repositories to your project after the declaration of plugins:
repositories {
maven { url "http://dl.bintray.com/populov/maven" }
mavenCentral()
}
Following the Antonio Leiva's tutorial on CollapsingToolbarLayout
Let's have the following layout (well boilerplate: /):
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:fitsSystemWindows="true">
<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" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="16dp" />
</FrameLayout>
</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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Some comments:
-
For the scrolling effect to work, you need to use components with the Nested Scroll
function. I know only 2: NestedScrollView
and RecyclerView
. I do not know if ListView
or GridView
works well without any customization.
-
Some components have some "crashes" during the Scroll, if this happens, I recommend the library smooth-app-bar-layout
that provides the same components with behavior improvements.
I think the setup of ViewPager
is known, but below is how to connect CirclePageIndicator
and ViewPager
:
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(...);
CirclePageIndicator circleIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
circleIndicator.setViewPager(pager);
Edit: Some feedback adjustments:
1) Addition of layout_gravity="bottom"
and layout_marginBottom="16p"
in CircleViewIndicator
.
2) Change from android:layout_height
from CollapsingToolbarLayout
to android:layout_height="wrap_content"
.
There are good references to the new components: