Android Mutiple View ViewPager With Centering

There is no real easy way to do this.  A quick hack I came up with was to find the ViewPager source code and go to private void scrollToItem and add the line:

destX = (int) (destX – (getWidth() –  getWidth()*mAdapter.getPageWidth(mCurItem))/2);

And it will center the current view with whatever you scale factor is in your page adapters overridden getPageWidth.

This probably won’t make any sense but when you use a viewpager normally and want to get the edges of the next and previous image then you’ll know.



About jjyap

I'm an undergraduate student at Simon Fraser University. A lot of the fun projects and research that I work on are Computer Vision and Machine Learning related.

5 responses to “Android Mutiple View ViewPager With Centering”

  1. Will Edward says :

    it’s a greate solution!!!Thanks a lot. I find a lots of solution on the internet , but your is the only way that works.

    but i find a problem on the last page of the ViewPager

    • jjyap says :

      Thank you, yes I got frustrated that there existed no solution to this problem. Yes you have to have a special case for the first and last one which I didn’t include

  2. Sepehr Behroozi says :

    Thanks man 🙂 works great
    if anyone wants to stick pages on the right side, just remove “/ 2” at the end of code snippet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: