Want to start animation on viewpager page but pages are preloading

前端 未结 2 525
天命终不由人
天命终不由人 2021-01-21 21:58

In my projects I have several screen(Fragments) and I want to give animation to the fragments elements(views). I have used view pager and viewpager adapter and fragments. The ma

2条回答
  •  醉酒成梦
    2021-01-21 22:27

    It is the default behavior of view-pager that it preload at-least 1 page from both side of current page. See following links

    ViewPager.setOffscreenPageLimit(0) doesn't work as expected

    So what you can do is Instead of starting animation onCreate start it from onPageScrolled callback

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
            //Loading fragment # position
    
            //Instead of starting animation from onCreate start it from here 
            //Only for the first time (for page 1) this callback will not trigger
        }
    
        @Override
        public void onPageSelected(int position) {
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    

    See setOffscreenPageLimit

    Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

    This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

    You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

提交回复
热议问题