简体   繁体   中英

When i setCurrentItem in the ViewPager, the ViewPager load data of 5 pages,why?

The ViewPager defaults to one page that should be retained to either side of the current page in the view hierarchy by the method setOffscreenPageLimit(). But when i setCurrentItem, it still load data of two front pages,this mean that it request data of 5 pages. How can i avoid this, just load data of 3 pages?

FragmentPagerAdapter adapter = new WorksFragmentAdapter(getSupportFragmentManager(),ids);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(currentItem);

Try overriding setUserVisibleHint in your Fragment and load your data inside it.

Here is an sample that works for me.

public class ViewPagerFragmentSample extends ListFragment{
    ...  
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            new AsyncListViewLoader().execute(); //Replace with your method
        }
    }
    ...
}

Hope this helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM