項(xiàng)目地址:
https://github.com/ogaclejapan/SmartTabLayout
介紹:
一個(gè)自定義的ViewPager tab指示爹凹,可以在滑動(dòng)的時(shí)候讓用戶感受到整個(gè)過程的UI反饋。
使用說明:
build.gradle.
dependencies {
compile 'com.ogaclejapan.smarttablayout:library:1.6.1@aar'
//Optional: see how to use the utility.
compile 'com.ogaclejapan.smarttablayout:utils-v4:1.6.1@aar'
//Optional: see how to use the utility.
compile 'com.ogaclejapan.smarttablayout:utils-v13:1.6.1@aar'
}
將SmartTabLayout控件放到布局中懈涛,一般來說是放在ViewPager的上面逛万。
<com.ogaclejapan.smarttablayout.SmartTabLayout
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="48dp"
app:stl_indicatorAlwaysInCenter="false"
app:stl_indicatorInFront="false"
app:stl_indicatorInterpolation="smart"
app:stl_indicatorColor="#40C4FF"
app:stl_indicatorThickness="4dp"
app:stl_indicatorCornerRadius="2dp"
app:stl_underlineColor="#4D000000"
app:stl_underlineThickness="1dp"
app:stl_dividerColor="#4D000000"
app:stl_dividerThickness="1dp"
app:stl_defaultTabTextAllCaps="true"
app:stl_defaultTabTextColor="#FC000000"
app:stl_defaultTabTextSize="12sp"
app:stl_defaultTabTextHorizontalPadding="16dp"
app:stl_defaultTabTextMinWidth="0dp"
app:stl_distributeEvenly="false"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/viewpagertab"
/>
在onCreate或者onCreateView中,將ViewPager和我們的控件綁定(如果你使用了工具目錄中的FragmentPagerItemAdapter批钠,你可以很容易田間item到PagerAdapter中)
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), FragmentPagerItems.with(this)
.add(R.string.titleA, PageFragment.class)
.add(R.string.titleB, PageFragment.class)
.create());
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
viewPagerTab.setViewPager(viewPager);
(可選) 如果你使用了OnPageChangeListener宇植,請將OnPageChangeListener設(shè)置到viewPagerTab,而不是直接調(diào)用ViewPager的setOnPageChangeListener埋心。
viewPagerTab.setOnPageChangeListener(mPageChangeListener);
(可選)使用 FragmentPagerItemAdapter指郁,可以方便的獲得Fragment所在的position
int position = FragmentPagerItem.getPosition(getArguments());
如何使用utils
Utility has two types available to suit the Android support library.
有兩個(gè)utils
utils-v4包含了適合android.support.v4.app.Fragment的PagerAdapter。
utils-v13包含了適合android.app.Fragment的PagerAdapter拷呆。
這兩個(gè)工具只是依賴不同闲坎,實(shí)現(xiàn)功能是一樣的。
基于view的PagerAdapter
ViewPagerItemAdapter adapter = new ViewPagerItemAdapter(ViewPagerItems.with(this)
.add(R.string.title, R.layout.page)
.add(ViewPagerItem.of("title", R.layout.page))
.create());
viewPager.setAdapter(adapter);
//...
public void onPageSelected(int position) {
//.instantiateItem() from until .destoryItem() is called it will be able to get the View of page.
View page = adapter.getPage(position);
}
基于Fragment的PagerAdapter
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), FragmentPagerItems.with(this)
.add(R.string.title, PageFragment.class),
.add(R.string.title, WithArgumentsPageFragment.class, new Bundler().putString("key", "value").get()),
.add(FragmentPagerItem.of("title", PageFragment.class))
.create());
viewPager.setAdapter(adapter);
//...
public void onPageSelected(int position) {
//.instantiateItem() from until .destoryItem() is called it will be able to get the Fragment of page.
Fragment page = adapter.getPage(position);
}