Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentStatePageAdapter: Multiple Items visible at the same time - How to center a specific page?

I'd like to use a FragmentStatePageAdapter and display 3 items at the same time. This could be done by overriding the getPageWidth()-Method:

    @Override
    public float getPageWidth(int position) {
        return 0.33f;
    }

This works great. But my problem is that I'd now like to have the centered page in focus. Currently the left page is in focus...

I've visualized the problem:

Currently:Current Pager

Desired: Desired Pager

Desired 2 (If you scroll until the end): Desired Page 2

What would you suggest?

like image 967
Frame91 Avatar asked Nov 01 '22 08:11

Frame91


1 Answers

Recapping the comment thread:

  • The issue regarding "focus" was due to the application of a PageTransformer. PageTransformer appears to be oblivious to getPageWidth() and will transform only the first visible page.

  • To center the "first" page, you will need an invisible page before that one. ViewPager always starts from the left, so the default would be to put the first page on the left. Having an invisible first page would put the first visible page in the next slot over. Note that depending on what behavior is desired for the end of the roster of pages, you may need one or more invisible pages there too.

like image 94
CommonsWare Avatar answered Nov 10 '22 00:11

CommonsWare