How to know viewpager is scroll left or right?

前端 未结 13 1817
梦谈多话
梦谈多话 2020-12-13 06:18

I am using ViewPager (support library). I want to know every time the ViewPager change the visible page, it is scrolling left or right.

Please give me a solution. An

13条回答
  •  醉梦人生
    2020-12-13 07:09

    You can keep class member variable to save last visited page

    private int mLastVisitedPageIndex = 0;
    

    Then use following function to check direction

    @Override
    public void onPageSelected(int i) {
        boolean isMovingForward = mLastVisitedPageIndex < i?true:false;
        //Use isMovingForward variable anywhere now
        mLastVisitedPageIndex = i;
    }
    

提交回复
热议问题