简体   繁体   中英

Java Android - ViewPager Fragment loading

I have ViewPager (4 Fragments), and when I launch app, these 4 fragments begin loading (json object from server), it not cool, because each fragments is about 100 items. How to make loading only one fragment at the start, and each loading only when the user switched to the fragment.

A similar application made ​​in Twitter, Vine.

By default the offscreenpagelimit is set to 1

ViewPager.getOffScreenPageLimit() == 1

So you only create two Fragments at once (if you didn't change the off screen limit, your user doesn't swipe and your first Fragment shown is the position 0).

If you would like to know what Fragment is currently shown, use

ViewPager.getCurrentItem()

If you need to know, when the user swipes to the next Fragment, use

ViewPager.setOnPageChangeListener(OnPageChangeListener listener)

If you need the instance of the Fragment, which is currently shown, use

shownFragment = FragmentPagerAdapter.getItem(ViewPager.getCurrentItem()); // you have to make sure to return the instance currently in use
shownFragment.doHeavyNetworkStuffInBackground();

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