MARM MARM - 1 year ago 146
Android Question

setCurrentItem in ViewPager not scroll immediately in the correct position

I use a PagerAdapter. I have a lot of elements so I don't load all elements in the same time. I reload the adapter, I change the old elements by the new elements. For example I have 4 elements, when I arrive at the last element, I reload the elements and I call setCurentItem(1) to be able to show the viewpager in position one. I have done a lot of tests. I put:

viewPager.setCurrentItem(1,false); //But not scroll, put drastically the new page.

I have tried to modify onPageScrollStateChanged(int arg0), I only have 3 views and always ubicate in the middle.

if (arg0 == ViewPager.SCROLL_STATE_DRAGGING) {
if(focusedPage==2) {
//Modify adapter

}else if(focusedPage==0){
//Modify adapter

And I have tryed to modify onPageSelected(int arg0)


Using this last example I have scroll the views immediately but always it located on the last page.
Anybody know how I can slide.

Thank you

Answer Source

Can I know more about the background of what you are trying to accomplish?

Anyway, from what I can understand you are trying to make an "infinite" ViewPager going from the last one to the first and vice versa (please correct me if I am wrong). And you want your ViewPager to go to the first page but scrolling - not just appearing from nowhere.

Well, if you disable the smooth scroll (the second parameter), then it will go directly to the page you want with no scroll motion (and will not "change" to the other pages on the way), and if you enable the smooth scroll then it will go through all the views, so you will see how it was not the next one but the first one.

    /*No swipe animation, and no onPageChanged for the page in the middle*/
    mPager.setCurrentItem(1, false); 

    /*The same as if you do not add the second parameter*/
    mPager.setCurrentItem(1, true);

    /*Your ViewPager scrolls through all the needed views until it arrives at that view.*/

What I can recommend to you with this in mind is that you can try to add "dummy" view at positions 0 and "last" and the view in position 0 would be the same as position "last - 1" and the "last" view would be the same as position "1". So you will do everything normal, EXCEPT that on the OnPageChangeListener you will check if the current page is the position "0" or "last" and if it is, you will call setCurrentItem("1", false) or setCurrentItem("last - 1", false) depending on which position you are at now. In this way you will keep the "page change" effect while accomplishing what you want.

I hope this can help you with your problem.

NOTE: It may have a laggy change after you arrive at the new position, because it will have to load the view for the current, next and last views (or if you are on 4.0+ then it will load only the actual position).


If you change the content of the List/Array that determines the content of the ViewPager, you need to call:

ViewPager mPager = new ViewPager();
mPager.getAdapter().notifyDataSetChanged();  //This notify that you need to recreate the views

/*This is if you want to change the data and then go to a specific position
If you do not do this you will have a very Buggy Behavior*/
new Handler().post(new Runnable() {
public void run() {
    mPager.setCurrentItem(2); //Where "2" is the position you want to go


Why do you not add all your elements to the ViewPager? the ViewPager already has optimization for handling those cases, in my case I have a lot of elements in it and it is working like a charm.

Another thing, in your code I see you change to the middle page each time a page is "settling". But then you really never show the right and left view, so why do you have them?

Last thing, when you change your dataset you need to call the "notifyDataSetChanged" method that I mentioned above, and then you call the "setCurrentItem" method inside a handler. If you don't, your app will not work as expected.


Well, the only thing I can recommend is having a List where you keep adding the new objects that you bring from the database, and then as long as you keep inserting objects to the end of the List there is no problem. If you want to add objects in the middle or at the beginning then you need to call the notifyDataSetChanged but that is going to be kind of slow because it will generate the views again. We do it this way and it works perfectly and we even download the info from a server, the data is not local (And actually we add info to the end and beginning of the List).

If you have any other questions about how to achieve this do not hesitate to ask.


Just in case anyone is actually looking for a circular ViewPager, I added a proof of concept code that you can re-use. It works without problem and I have used it with complex views and there is still no lag whatsoever.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download