Perpustakaan Desain Material ViewPager mudah digunakan
Dan lihat contoh Video Youtube : Tautan Youtube
Dalam modul Anda
compile ' com.github.florent37:materialviewpager:1.2.3 '
// dependencies
compile ' com.flaviofaria:kenburnsview:1.0.7 '
compile ' com.jpardogo.materialtabstrip:library:1.1.0 '
compile ' com.github.bumptech.glide:glide:4.0.0 '
Tambahkan MaterialViewPager ke tata letak aktivitas Anda
< com .github.florent37.materialviewpager.MaterialViewPager
android : id = " @+id/materialViewPager "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
app : viewpager_logo = " @layout/header_logo "
app : viewpager_logoMarginTop = " 100dp "
app : viewpager_color = " @color/colorPrimary "
app : viewpager_headerHeight = " 200dp "
app : viewpager_headerAlpha = " 1.0 "
app : viewpager_hideLogoWithFade = " false "
app : viewpager_hideToolbarAndTitle = " true "
app : viewpager_enableToolbarElevation = " true "
app : viewpager_parallaxHeaderFactor = " 1.5 "
app : viewpager_headerAdditionalHeight = " 20dp "
app : viewpager_displayToolbarWhenSwipe = " true "
app : viewpager_transparentToolbar = " true "
app : viewpager_animatedHeaderImage = " true "
app : viewpager_disableToolbar = " false "
/>
dengan header_logo.xml
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< TextView xmlns : android = " http://schemas.android.com/apk/res/android "
android : id = " @+id/logo_white "
android : layout_width = " wrap_content "
android : layout_height = " @dimen/materialviewpager_logoHeight "
android : text = " Material is Good "
android : textSize = " 30sp "
android : textColor = " @android:color/white " />
Anda akan melihat di Pratinjau Android Studio :
Untuk mendapatkan layar yang indah dan mengaktifkan pratinjau, tema Anda dapat mengikuti
< style name = " AppBaseTheme " parent = " @style/Theme.AppCompat.Light " >
</ style >
< style name = " AppTheme " parent = " AppBaseTheme " >
< item name = " android:textColorPrimary " >@android:color/white</ item >
< item name = " drawerArrowStyle " >@style/DrawerArrowStyle</ item >
< item name = " android:windowTranslucentStatus " tools : targetApi = " 21 " >true</ item >
< item name = " android:windowContentOverlay " >@null</ item >
< item name = " windowActionBar " >false</ item >
< item name = " windowNoTitle " >true</ item >
<!-- Toolbar Theme / Apply white arrow -->
< item name = " colorControlNormal " >@android:color/white</ item >
< item name = " actionBarTheme " >@style/AppTheme.ActionBarTheme</ item >
<!-- Material Theme -->
< item name = " colorPrimary " >@color/colorPrimary</ item >
< item name = " colorPrimaryDark " >@color/colorPrimaryDark</ item >
< item name = " colorAccent " >@color/accent_color</ item >
< item name = " android:navigationBarColor " tools : targetApi = " 21 " >@color/navigationBarColor</ item >
< item name = " android:windowDrawsSystemBarBackgrounds " tools : targetApi = " 21 " >true</ item >
</ style >
< style name = " AppTheme.ActionBarTheme " parent = " @style/ThemeOverlay.AppCompat.ActionBar " >
<!-- White arrow -->
< item name = " colorControlNormal " >@android:color/white</ item >
</ style >
< style name = " DrawerArrowStyle " parent = " Widget.AppCompat.DrawerArrowToggle " >
< item name = " spinBars " >true</ item >
< item name = " color " >@color/drawerArrowColor</ item >
</ style >
Anda dapat menggunakan MaterialViewPager sebagai Tampilan Android biasa, dan mendapatkannya dengan findViewById
public class MainActivity extends ActionBarActivity {
private MaterialViewPager mViewPager ;
@ Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
mViewPager = ( MaterialViewPager ) findViewById ( R . id . materialViewPager );
}
}
Pertama pilih warna dan tinggi badan Anda
< com .github.florent37.materialviewpager.MaterialViewPager
android : id = " @+id/materialViewPager "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
...
app : viewpager_color = " @color/colorPrimary "
app : viewpager_headerHeight = " 200dp "
...
/>
< com .github.florent37.materialviewpager.MaterialViewPager
...
app : viewpager_logo = " @layout/header_logo " <-- look custom logo layout
app : viewpager_logoMarginTop = " 100dp " <-- look at the preview
...
/>
Tata letak logo Anda harus
header_logo.xml
< ImageView xmlns : android = " http://schemas.android.com/apk/res/android "
android : id = " @+id/logo_white "
android : layout_width = " 200dp "
android : layout_height = " @dimen/materialviewpager_logoHeight "
android : fitsSystemWindows = " true "
android : adjustViewBounds = " true "
android : layout_centerHorizontal = " true "
android : src = " @drawable/logo_white " />
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_hideLogoWithFade = " false "
...
/>
header_logo.xml
< FrameLayout xmlns : android = " http://schemas.android.com/apk/res/android "
android : layout_width = " 80dp "
android : layout_height = " 80dp "
android : layout_centerHorizontal = " true "
android : background = " @drawable/circle " >
< ImageView
android : layout_width = " 30dp "
android : layout_height = " 30dp "
android : fitsSystemWindows = " true "
android : adjustViewBounds = " true "
android : layout_gravity = " center "
android : src = " @drawable/flying " />
</ FrameLayout >
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_hideLogoWithFade = " true "
...
/>
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_hideToolbarAndTitle = " true "
...
/>
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_hideToolbarAndTitle = " false "
...
/>
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_transparentToolbar = " true "
...
/>
Anda dapat mengganti headernya
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_header = " @layout/myHeader "
...
/>
Atau gunakan header default, dengan animasi KenBurns
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_animatedHeaderImage = " true "
...
/>
Atau cukup gunakan ImageView sebagai header
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_animatedHeaderImage = " false "
...
/>
Anda dapat mengatur bilah tab Anda sendiri, secara default saya menyediakan 2 implementasi
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_pagerTitleStrip = " @layout/material_view_pager_pagertitlestrip_standard "
...
/>
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_pagerTitleStrip = " @layout/material_view_pager_pagertitlestrip_newstand "
...
/>
Buat tata letak Anda sendiri menggunakan PagerSlidingTabStrip
tab_saya.xml
< com .astuetz.PagerSlidingTabStrip
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 : id = " @id/materialviewpager_pagerTitleStrip "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
app : pstsPaddingMiddle = " true "
app : pstsDividerPadding = " 20dp "
app : pstsIndicatorColor = " #FFF "
app : pstsIndicatorHeight = " 2dp "
app : pstsShouldExpand = " true "
app : pstsTabPaddingLeftRight = " 10dp "
app : pstsTabTextAllCaps = " true "
tools : background = " #A333 "
/>
Jangan lupa kasih id="@id/materialviewpager_pagerTitleStrip"
< com .github.florent37.materialviewpager.MaterialViewPager
...
app : viewpager_pagerTitleStrip = " @layout/my_tabs "
...
/>
Cukup tambahkan mendengarkan ke ViewPager
mViewPager . setMaterialViewPagerListener ( new MaterialViewPager . Listener () {
@ Override
public HeaderDesign getHeaderDesign ( int page ) {
switch ( page ) {
case 0 :
return HeaderDesign . fromColorResAndUrl (
R . color . blue ,
"http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2014/06/wallpaper_51.jpg" );
case 1 :
return HeaderDesign . fromColorResAndUrl (
R . color . green ,
"https://fs01.androidpit.info/a/63/0e/android-l-wallpapers-630ea6-h900.jpg" );
case 2 :
return HeaderDesign . fromColorResAndUrl (
R . color . cyan ,
"http://www.droid-life.com/wp-content/uploads/2014/10/lollipop-wallpapers10.jpg" );
case 3 :
return HeaderDesign . fromColorResAndUrl (
R . color . red ,
"http://www.tothemobile.com/wp-content/uploads/2014/07/original.jpg" );
}
//execute others actions if needed (ex : modify your header logo)
return null ;
}
});
Tersedia
HeaderDesign . fromColorAndUrl ( Color . BLUE , "http:...);
HeaderDesign.fromColorResAndUrl(R.color.blue," http :...);
HeaderDesign . fromColorAndDrawable ( Color . BLUE , myDrawable );
HeaderDesign . fromColorResAndDrawable ( R . color . blue , myDrawable );
Toolbar toolbar = mViewPager . getToolbar ();
if ( toolbar != null ) {
setSupportActionBar ( toolbar );
ActionBar actionBar = getSupportActionBar ();
actionBar . setDisplayHomeAsUpEnabled ( true );
actionBar . setDisplayShowHomeEnabled ( true );
actionBar . setDisplayShowTitleEnabled ( true );
actionBar . setDisplayUseLogoEnabled ( false );
actionBar . setHomeButtonEnabled ( true );
}
ViewPager viewPager = mViewPager . getViewPager ();
viewPage . setAdapter (...);
//After set an adapter to the ViewPager
mViewPager . getPagerTitleStrip (). setViewPager ( mViewPager . getViewPager ());
mRecyclerView . addItemDecoration ( new MaterialViewPagerHeaderDecorator ());
mRecyclerView . setAdapter ( yourAdapter );
ScrollView harus berupa NestedScrollView`
MaterialViewPagerHelper . registerScrollView ( getActivity (), mScrollView , null );
Dan sertakan @layout/material_view_pager_placeholder` sebagai anak pertama
< android .support.v4.widget.NestedScrollView
xmlns : android = " http://schemas.android.com/apk/res/android "
android : id = " @+id/scrollView "
android : layout_width = " match_parent "
android : layout_height = " match_parent " >
< LinearLayout
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : orientation = " vertical " >
< include layout = " @layout/material_view_pager_placeholder " />
...your content...
</ LinearLayout >
</ android .support.v4.widget.NestedScrollView>
Tetap :
Memperbaiki : Pengguliran cepat menghasilkan ketinggian Toolbar yang bervariasi
RecyclerViewMaterialAdapter dapat menangani jumlah sel placeholder khusus (berguna untuk GridLayoutManager)
public RecyclerViewMaterialAdapter ( RecyclerView . Adapter adapter , int placeholderSize )
Atribut yang ditambahkan
app : viewpager_parallaxHeaderFactor = "1.5"
app : viewpager_headerAdditionalHeight = "20dp"
parallaxHeaderFactor Memodifikasi kecepatan gulir header paralaks (bukan kecepatan efek KenBurns) parallaxHeaderFactor Mengatur ketinggian tata letak header yang ditampilkan di belakang tampilan kartu pertama
Memperbaiki masalah saat menggulir ke bawah & menggulir ke atas berkali-kali saat hideToolbarAndTitle="true"
Atribut yang ditambahkan
viewpager_headerAlpha = "0.6"
Mencari kontributor, jangan ragu untuk bercabang!
Beritahu saya jika Anda menggunakan perpustakaan saya di aplikasi Anda, saya akan membagikannya di README ini
Penulis: Florent Champigny http://www.florentchampigny.com/
Blog : http://www.tutos-android-france.com/
Fiches Plateau Moto : https://www.fiches-plateau-moto.fr/
Copyright 2015 florent37, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.