Android ViewPager - Show preview of page on left and right

后端 未结 10 2581
旧巷少年郎
旧巷少年郎 2020-12-02 03:52

I\'m using Android\'s ViewPager. What I want to do is to show a preview of the page on both the left and the right. I\'ve seen where I can use a negative

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 04:39

    The answer by @JijuInduchoodan is perfect and working. However, since I am relatively new to Android, it took me a while to understand & set it properly. So, I am posting this answer for future reference and help anyone else who is in same shoes as me.

    if (viewPager == null) 
            {
    
                // Initializing view pager
                viewPager = (ViewPager) findViewById(R.id.vpLookBook);
    
                // Disable clip to padding
                viewPager.setClipToPadding(false);
                // set padding manually, the more you set the padding the more you see of prev & next page
                viewPager.setPadding(40, 0, 40, 0);
                // sets a margin b/w individual pages to ensure that there is a gap b/w them
                viewPager.setPageMargin(20);
            }
    

    There's no need to set any width to the ViewPager's page in the adapter. There no additional code required to see previous & next page in ViewPager. However, if you want to add blank space at the top & bottom of the each page, you can set the following code to ViewPager's child page`s parent layout.

    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    

    This will be the final look of the ViewPager.

提交回复
热议问题