사용하기 쉬운 머티리얼 디자인 ViewPager 라이브러리
샘플 YouTube 동영상을 살펴보세요. YouTube 링크
귀하의 모듈에서
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 '
활동 레이아웃에 MaterialViewPager 추가
< 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 "
/>
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 " />
Android Studio 미리보기에서 다음을 확인할 수 있습니다.
아름다운 화면을 얻고 미리보기를 활성화하려면 테마를 따를 수 있습니다.
< 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 >
MaterialViewPager를 일반적인 Android View로 사용하고 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 );
}
}
먼저 색상과 키를 선택하세요.
< 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
...
/>
로고 레이아웃은 다음과 같아야 합니다.
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 "
...
/>
헤더를 교체할 수 있습니다.
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_header = " @layout/myHeader "
...
/>
또는 KenBurns 애니메이션과 함께 기본 헤더를 사용하세요.
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_animatedHeaderImage = " true "
...
/>
또는 단순히 ImageView를 헤더로 사용하십시오.
< com .github.florent37.materialviewpager.MaterialViewPager`
...
app : viewpager_animatedHeaderImage = " false "
...
/>
자신만의 탭 표시줄을 설정할 수 있습니다. 기본적으로 2가지 구현을 제공했습니다.
< 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 "
...
/>
PagerSlidingTabStrip을 사용하여 나만의 레이아웃 만들기
my_tabs.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 "
/>
id="@id/materialviewpager_pagerTitleStrip"을 제공하는 것을 잊지 마세요.
< com .github.florent37.materialviewpager.MaterialViewPager
...
app : viewpager_pagerTitleStrip = " @layout/my_tabs "
...
/>
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 ;
}
});
사용 가능
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는 NestedScrollView여야 합니다.
MaterialViewPagerHelper . registerScrollView ( getActivity (), mScrollView , null );
그리고 @layout/material_view_pager_placeholder`를 첫 번째 하위 항목으로 포함합니다.
< 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>
수정됨:
수정됨: 빠른 스크롤로 인해 도구 모음 높이가 다양해짐
RecyclerViewMaterialAdapter는 사용자 정의 자리 표시자 셀 수를 처리할 수 있습니다(GridLayoutManager에 유용함)
public RecyclerViewMaterialAdapter ( RecyclerView . Adapter adapter , int placeholderSize )
추가된 속성
app : viewpager_parallaxHeaderFactor = "1.5"
app : viewpager_headerAdditionalHeight = "20dp"
parallaxHeaderFactor 시차 헤더 스크롤 속도 수정(KenBurns 효과 속도 아님) parallaxHeaderFactor 첫 번째 카드 보기 뒤에 표시되는 헤더 레이아웃의 높이 설정
hideToolbarAndTitle="true"일 때 여러 번 아래로 스크롤하고 위로 스크롤할 때 발생하는 문제가 해결되었습니다.
추가된 속성
viewpager_headerAlpha = "0.6"
기여자를 찾고 있다면 자유롭게 포크해 보세요!
귀하의 애플리케이션에서 내 라이브러리를 사용하고 있다면 알려주세요. 이 README에서 공유하겠습니다.
저자: 플로랑 샴피니 http://www.florentchampigny.com/
블로그 : http://www.tutos-android-france.com/
피쉬스 고원 모토 : 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.