回答

0

首先,創建自己的監聽器,正是從View.OnScrollChangeListener

public interface CustomOnScrollChangeListener { 
    /** 
    * Called when the scroll position of a view changes. 
    * 
    * @param v The view whose scroll position has changed. 
    * @param scrollX Current horizontal scroll origin. 
    * @param scrollY Current vertical scroll origin. 
    * @param oldScrollX Previous horizontal scroll origin. 
    * @param oldScrollY Previous vertical scroll origin. 
    */ 
    void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY); 
} 

得到它,那麼你可以創建自定義ViewPager

public class CustomPager extends ViewPager { 
    private int mScrollingX, mScrollingY; 
    private CustomOnScrollChangeListener mListener; 

    public CustomPager(Context context) { 
     super(context); 
    } 

    public CustomPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public void setCustomScrollChangeListener(CustomOnScrollChangeListener listener) { 
     mListener = listener; 
    } 

    @Override 
    public void scrollTo(@Px int x, @Px int y) { 
     super.scrollTo(x, y); 
     if (mScrollingX != x || mScrollingY != y) { 
      int oldX = mScrollingX; 
      int oldY = mScrollingY; 
      mScrollingX = x; 
      mScrollingY = y; 
      invalidate(); 
      mListener.onScrollChange(this, mScrollingX, mScrollingY, oldX, oldY); 
      if (!awakenScrollBars()) { 
       postInvalidateOnAnimation(); 
      } 
     } 
    } 
} 

而且現在你可以開始使用它了。

mViewPager.setCustomScrollChangeListener(new CustomOnScrollChangeListener() { 
      @Override 
      public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, 
        int oldScrollY) { 
       //Put your logic code here 
      } 
     }); 
相關問題