Automatic Scrolling View pager page with few interval

1.6k Views Asked by At

I have used android view pager to display images and text now what i want is that smothly scrolling pages after few seconds and with animation?

Its works but not smothly viewpager pages scroll.

Timer call

  mCountDownTimer = new MyCountDownTimer(4000, 1000);
  mCountDownTimer.start();

.

 private static MyCountDownTimer mCountDownTimer;
  private class MyCountDownTimer extends CountDownTimer {
    public MyCountDownTimer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        System.out.println("Time's up!");
        startTimerWithAnim();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        System.out.println("mill="+millisUntilFinished / 1000);
    }
}

if the 'MyCountDownTimer' call then the 'startTimerWithAnim' method call

      public static void startTimerWithAnim() {
       if (mCountDownTimer != null)
          mCountDownTimer.cancel();
        mHandler = new Handler();
        mRunnable = new Runnable() {
          public void run() {
              if (mMemberPagerAdapterList != null) {
                  if (mPagedGrid.getCurrentItem() == mMemberPagerAdapterList.getCount() - 1)
                    mPagedGrid.setCurrentItem(0, true);
                else if (isFirstTimeCurrentItem) {
                    mPagedGrid.setCurrentItem(0, true);
                    isFirstTimeCurrentItem = false;
                } else if (isFirstTimeCurrentItem == false)
                    mPagedGrid.setCurrentItem(mPagedGrid.getCurrentItem() + 1, true);
            }
        }
    };

    mTimer = new Timer();
    mTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            mHandler.post(mRunnable);
        }
    }, 100, 4000);
}
2

There are 2 best solutions below

0
On

There you go!

  class UpdateTimeTask extends TimerTask {
                       public void run() {
                           viewPager1.post(new Runnable() {
                                public void run() {

                                    if(viewPager1.getCurrentItem()<i){
                                    viewPager1.setCurrentItem(viewPager1.getCurrentItem()+1,true);
                                    String abc = String.valueOf(viewPager1.getCurrentItem());
                                    Log.i("timer_+",abc);
                                    }
                                    else
                                    {
                                        viewPager1.setCurrentItem(0,true);
                                    }
                                } 
                            });
                      }
                     }

                    Timer timer = new Timer();
                     timer.schedule(new UpdateTimeTask(), 2000, 4000);
0
On
function doSomething() {
  $(document).scrollTop($(document).height());
}
setInterval(doSomething, 5000);

This will scroll to bottom of the page every 5 seconds. This will be useful, if you have auto load content when user scroll down (like Facebook).