简体   繁体   English

Java Android-ViewPager片段加载

[英]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. 我有ViewPager(4个片段),当我启动应用程序时,这4个片段开始加载(来自服务器的json对象),它并不酷,因为每个片段大约有100个项目。 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. 在Vine的Twitter中制作了一个类似的应用程序。

By default the offscreenpagelimit is set to 1 默认情况下,offscreenpagelimit设置为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). 因此,您一次只能创建两个片段(如果不更改屏幕外限制,则用户不会滑动,并且显示的第一个片段的位置为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 如果您需要当前显示的Fragment实例,请使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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