1

I am developing an android app, where i want to create a slideshow of screens.I am making use of the viewpager for swiping between the screens, and making use of java Timer to automatic slideshow for every 5 seconds.

The problem is, i am not able get a slow and smooth transition from one screen to another. As soon as the 5 seconds is completed ,it immediately moves to the next screen.i need a slow and smooth transition.Is the possible?.Please check out my timer code & view pager code below.

     public void slideshowtimer()
     {
      t.scheduleAtFixedRate(new TimerTask() 
       {

        @Override
        public void run() 
        {
            runOnUiThread(new Runnable() 
            {

                public void run() 
                {
                    Log.e("inside", "timer - "  + viewPagerCurrentItem);

                    myPager.setCurrentItem(viewPagerCurrentItem++);

                }

            });
        }

    }, 0, 5000);
}



              @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    t = new Timer();

    MyPagerAdapter adapter = new MyPagerAdapter();
    myPager = (ViewPager) findViewById(R.id.viewPager);
    myPager.setAdapter(adapter);

    slideshowtimer();

    //myPager.setCurrentItem(0);
}





private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:                         

            //  resId = LayoutOne.newInstance(_context);    

            resId = R.layout.layout_one;
            break;
        case 1:
            resId = R.layout.layout_two;
            break;
        case 2:
            resId = R.layout.layout_third;
            break;

        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

Please help.Thanks!

DevAndro
  • 205
  • 1
  • 6
  • 18
  • Try this link for Auto Scroll of Viewpager. http://stackoverflow.com/a/19951862/2987284 – Raghu Nov 19 '13 at 12:59

2 Answers2

1

Have you tried setting smooth Scroll to true? Look at the method here.

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • i tried it now, but no difference.the transition i still immediate from one screen to another. – DevAndro May 31 '13 at 07:47
  • this is what i tried.. _mViewPager.setCurrentItem(viewPagerCurrentItem++,true); – DevAndro May 31 '13 at 07:49
  • There seems to be a problem with the method, have a look at this question: http://stackoverflow.com/questions/11962268/viewpager-setcurrentitempageid-true-does-not-smoothscroll Other than that, you could try to not use a ViewPager but to replace fragments with FragmentTransaction and set an animation to it. – fweigl May 31 '13 at 07:55
1

Have you tried adding your own scroller to the ViewPager. You can set scroll animation to appear using the duration in scroll class. I did it like this:

viewFlow=(ViewPager)findViewById(R.id.presentationViewPager);
try {
        Field mScroller;
        Interpolator sInterpolator = new DecelerateInterpolator();
        mScroller = ViewPager.class.getDeclaredField("mScroller");
        mScroller.setAccessible(true); 
        FixedSpeedScroller scroller = new FixedSpeedScroller(viewFlow.getContext(),   sInterpolator);
        // scroller.setFixedDuration(5000);
        mScroller.set(viewFlow, scroller);
    } catch (NoSuchFieldException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }

And here is my Scroller code.:

public class FixedSpeedScroller extends Scroller {

private int mDuration = 600;

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

public FixedSpeedScroller(Context context, Interpolator interpolator) {
    super(context, interpolator);
}

public FixedSpeedScroller(Context context,Interpolator interpolator, boolean flywheel) {
    super(context, interpolator, flywheel);
}


@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
    // Ignore received duration, use fixed one instead
    super.startScroll(startX, startY, dx, dy, mDuration);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy) {
    // Ignore received duration, use fixed one instead
    super.startScroll(startX, startY, dx, dy, mDuration);
}

}

Sachin
  • 217
  • 1
  • 4
  • 12