給ViewPager加上滑動條


使用網易新聞的時候,如果左右滑動頁面,會發現上面的Tab下面有條紅條,可以隨着下面頁面的滑動而滑動,用來指明當前的頁面。研究了一下,發現可以使用ViewPager和自定義View來實現類似的效果。

        在使用Viewpager的時候,我們一般都會註冊一個OnPageChangeListener,來看一下它的代碼:
   點擊(此處)摺疊或打開
  1. /**
  2.      * Callback interface for responding to changing state of the selected page.
  3.      */
  4.     public interface OnPageChangeListener {

  5.         /**
  6.          * This method will be invoked when the current page is scrolled, either as part
  7.          * of a programmatically initiated smooth scroll or a user initiated touch scroll.
  8.          *
  9.          * @param position Position index of the first page currently being displayed.
  10.          * Page position+1 will be visible if positionOffset is nonzero.
  11.          * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
  12.          * @param positionOffsetPixels Value in pixels indicating the offset from position.
  13.          */
  14.         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);

  15.         /**
  16.          * This method will be invoked when a new page becomes selected. Animation is not
  17.          * necessarily complete.
  18.          *
  19.          * @param position Position index of the new selected page.
  20.          */
  21.         public void onPageSelected(int position);

  22.         /**
  23.          * Called when the scroll state changes. Useful for discovering when the user
  24.          * begins dragging, when the pager is automatically settling to the current page,
  25.          * or when it is fully stopped/idle.
  26.          *
  27.          * @param state The new scroll state.
  28.          * @see ViewPager#SCROLL_STATE_IDLE
  29.          * @see ViewPager#SCROLL_STATE_DRAGGING
  30.          * @see ViewPager#SCROLL_STATE_SETTLING
  31.          */
  32.         public void onPageScrollStateChanged(int state);
  33.     }
    可以看到這個接口有三個回調的方法,其中onPageScrolled方法在滑動的時候會被調用。這個方法有三個參數,看其說明可以知道 position就是當前顯示第一頁的序號,positionOffset是一個0到1的數,用來表示當前頁滑動的位置,數值越大,就表示滑動的幅度越大。就需要通過這個方法來知道滑動的位置。

   下面需要一個自定義的View,來畫這個滑動條。當onPageScrolled方法被調用的時候,我們就需要重畫來定位滑動條的位置。下面是自定義VIew的代碼:

點擊(此處)摺疊或打開

  1. public class ScllorTabView extends View {

  2.     private int mTabNum, mCurrentNum;
  3.     private float mWidth, mTabWidth, mOffset;
  4.     private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  5.     private int mBeginColor;
  6.     private int mEndColor;
  7.     LinearGradient gradient;

  8.     public ScllorTabView(Context context, AttributeSet attrs) {
  9.         super(context, attrs);
  10.     }

  11.     public void setTabNum(int n) {
  12.         mTabNum = n;
  13.     }

  14.     public void setCurrentNum(int n) {
  15.         mCurrentNum = n;
  16.         mOffset = 0;
  17.     }

  18.     public void setOffset(int position, float offset) {
  19.         if (offset == 0) {
  20.             return;
  21.         }
  22.         mCurrentNum = position;
  23.         mOffset = offset;
  24.         invalidate();
  25.     }

  26.     @Override
  27.     protected void onDraw(Canvas canvas) {
  28.         super.onDraw(canvas);
  29.         if (mTabWidth == 0) {
  30.             mWidth = getWidth();
  31.             mTabWidth = mWidth / mTabNum;
  32.         }
  33.         //根據位置和偏移量來計算滑動條的位置
  34.         float left = (mCurrentNum + mOffset) * mTabWidth;
  35.         final float right = left + mTabWidth;
  36.         final float top = getPaddingTop();
  37.         final float bottom = getHeight() - getPaddingBottom();

  38.         // if (gradient == null) {
  39.         LinearGradient gradient = new LinearGradient(left, getHeight(), right,
  40.                 getHeight(), mBeginColor, mEndColor, Shader.TileMode.CLAMP);
  41.         mPaint.setShader(gradient);
  42.         // }
  43.         canvas.drawRect(left, top, right, bottom, mPaint);
  44.     }

  45.     public void setSelectedColor(int color, int color2) {
  46.         mBeginColor = color;
  47.         mEndColor = color2;

  48.     }
  49. }
    其中LinearGradient是用來設置漸變色的,也可以去掉。最後只需要加上調用更新的代碼就可以了:

點擊(此處)摺疊或打開

  1. @Override
  2.             public void onPageScrolled(int position, float positionOffset,
  3.                     int positionOffsetPixels) {
  4.                 mScllorTabView.setOffset(position, positionOffset);
  5.             }
    好了,來看一下實現的效果吧



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章