Starting from the beginning, the new Google library, Design Library
brings several ready-made components with some common interactions of Material Design.
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 the Design Library
comes another 3 that it also depends on... If you already use them, no problem. But you can bring an overhead to your app if you don’t use them.
In addition, to implement the indicators I recommend using the ViewPagerIndicator
of Jake Wharton.
With this, dependency has to be included as well:
compile 'com.viewpagerindicator:library:2.4.1@aar'
But since Jakewharton didn’t publish his library as an AAR, you need to include the following repositories to your project, after the plugin declaration:
repositories {
maven { url "http://dl.bintray.com/populov/maven" }
mavenCentral()
}
Following the tutorial of Antonio Leiva on the CollapsingToolbarLayout
We will 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 remarks:
For the scroll effect to work, you need to use components with the Nested Scroll
. I only know 2: NestedScrollView
and the RecyclerView
. I don’t know if the ListView
or GridView
work well without any customization.
Some components have some "crashes" during Scroll, if this occurs, recommend the library smooth-app-bar-layout
that provides the same components with improved behavior.
I believe the setup of ViewPager
be known, but below follows the way to connect the CirclePageIndicator
and the ViewPager
:
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(...);
CirclePageIndicator circleIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
circleIndicator.setViewPager(pager);
Edit: Some adjustments as feedback:
1) Addition of layout_gravity="bottom"
and layout_marginBottom="16p"
in the CircleViewIndicator
.
2) Exchange of android:layout_height
of CollapsingToolbarLayout
for android:layout_height="wrap_content"
.
There are good references about the new components:
The doubt itself is to make that indicator attached to the
ViewPager
?– Wakim
@Wakim The indicator and the view pager with material design effect together with Toolbar.
– Lucas Santos
Hmmm, that would be the case effect with the
ViewPager
instead ofImageView
?– Wakim
@Wakim would be EXACTLY this effect that put on the link only with the Image Viewpager with those balls, but the effect is exactly this, the layout. I would like to help with these effects because I’m very new to Material Design. Even in my layout will have a "Fab" of "+" attached to the viewpager exactly as in the link.
– Lucas Santos
This article will give a good basis, but I never tried to change the
ImageView
forViewPager
, but I think it might work... I can put an answer with the basic setup. And in this case, for the indicator I recommend using the Viewpagerindicator of Jake Wharton.– Wakim
I’ll take a look at the bilbioteca.
– Lucas Santos