1.下載和解壓 下載地址: https://github.com/JakeWharton/ViewPagerIndicatorandroid
2.運行案例git
3.當前項目關聯庫github
4.寫佈局文件 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layoutwidth="fillparent"
android:layoutheight="fillparent">ide
<com.viewpagerindicator.TabPageIndicator android:id="@+id/indicator" android:background="@drawable/base_action_bar_bg" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
5.使用佈局
//實例化TabPageIndicator而後設置ViewPager與之關聯 TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator); indicator.setViewPager(pager); //若是咱們要對ViewPager設置監聽,用indicator設置就好了
6.在適配器中多寫spa
@Override public CharSequence getPageTitle(int position) { return children.get(position).getTitle(); }
7.設置樣式,在工程的功能清單文件,對應的Activity配置樣式 <activity android:name=".MainActivity" android:theme="@style/Theme.PageIndicatorDefaults"/>code
8.修改樣式xml
9.修改後的 @drawable/vpi_tabindicator事件
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/news_tab_item_bg_select" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/news_tab_item_bg_select" /> <!-- Pressed --> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@android:color/transparent" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/news_tab_item_bg_select" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@android:color/transparent" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/news_tab_item_bg_select" />
10.文字顏色get
@drawable/vpitabtextcolorindicator
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:color="#000000" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:color="#ff0000" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:color="#000000" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:color="#ff0000" /> <!-- Pressed --> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:color="#000000" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:color="#ff0000" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:color="#000000" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:color="#ff0000" /> </selector>
一.豎直方向滑動,不作處理 設置是否攔截事件爲 getParent().requestDisallowInterceptTouchEvent(false);
二.水平方向滑動 1.當滑動到第一個頁面,而且方向是從左到右的滑動 endX - startX > 0 那麼方向就是:從左往右滑動 getParent().requestDisallowInterceptTouchEvent(false);
2.當滑動到最後一個頁面的時候,而且方向是從右到左滑動 endX - startX < 0 那麼方向就是:從右往左滑動 getParent().requestDisallowInterceptTouchEvent(false);
3.其餘狀況 getParent().requestDisallowInterceptTouchEvent(true);