I'm using ViewPager in Activity, whose adapter is a subclass of FragmentStatePagerAdapter. I have only three pages and each page show a view generated by Fragment(not complicated view).
My issue is: every time when I load the viewpager, I have to wait nearly 3 seconds before I can swipe left/right. The time interval between onCreate and onResume is less than 0.3 second
My question is: what's the potential root causes for this kind of delay?
Thanks
If you could post some code that would be great, but if i was going to take a stab...
I would say that you have many items in the adapter, and you have set the setOffscreenPageLimit(int) too high, and the viewpager is loading the fragments of too many pages.
Related
I have several pages some 8 pages on my dashboard containing viewpager and all have recycler view on it. The problem is when I swipe through viewpager, both swiping and recycler view setting adapter on the page happens at the same time on same ui thread due to which swiping lags and hence there is no smooth swiping and loading experience.
Things I tried using:
1. I tried putting delay to load the data but when swiping through, the previous fragment loads data due to which the ui lags again.
2. I also used setoffscreenpagelimit but I have some 8 pages and loading all at once or even some takes time to open the dashboard.
3. I tried using addOnPageChangeListener of the viewpager. But it lags the most.
Currently i am loading data when fragment is visible i.e in setUserVisibleHint method. I want to have a smooth swiping and loading experience. Help needed.
Thanks very much.
android View pager load two pages at a time into memory.i want to load one page at a time and call when page scrolled is it possible?
how can i load one page at a time when user scrolled then call second fragment?
actually it is 3. view pager always load 3 pages or to be exact 3 fragments into memory. this insures user doesn't encounter a frame drop. but there is a catch:
we have two kind of view pager adapter: FragmentPagerAdapter and FragmentStatePagerAdapter .
if you have fragments with heavily content i suggest FragmentStatePagerAdapter because it doesn't load all 3 fragments together. it loads the first one and the save only the states of the other two. its better in case you want to manage memory for heavily content Fragments
I have a viewpager in my layout , that viewpager holds a set of 10 fragments . Each fragment has a list view , which is asynchronously populated . I m currently using FragmentPagerAdapter as adapter for the viewpager and the API calls to populate the list view is done in onCreateView of each fragment . The swipe is insanely slow and app closes itself because of the memory issues .
How to achieve smooth and responsive (viewpager+listview) like Google Play does ?
Smooth swiping + good cache of list items ?
There are many ways to improve the performance of a ViewPager integrated with a ListView.
First, change the PagerAdapter from FragmentPagerAdapter to FragmentStatePagerAdapter. The difference is that you load 10 pages within the pager. And if you went through all the manager will only put the pages in onStop() therefore it will reserve space. However using FragmentStatePagerAdapter will destroy these pages but will save their instance using the onSaveInstanceState method. So when you get back it won't take much time to load and you will be saving memory.
Usually FragmentPagerAdapter is used with 3 pages or less.
The second way to improve performance (but it drains the battery faster) is adding this line to your AndroidManifest.xml under your application tag:
android:hardwareAccelerated="true"
The third way is detecting the swipe gesture. You can do this by implementing onPageChangeListener then in the implemented method onPageScrollStateChanged you check if the page is Idle then add the ListView. Otherwise, stop the use of ListView and scroll to the next page. Here is a better explanation of this point.
Hope some of these points can help you out to achieve better performance.
I'm using ViewPager that holds 3 fragments, each fragment loads a list. The problem is when the application runs for the first time and I swipe to the next fragment, this fragment needs sometime to load (about 2 seconds) before its view is visible. This is a very weird behavior. All I want is once the app has started, all fragments in ViewPager should be ready for user so when they swipe through fragments, there's no wait time. How can I do that?
Just call setOffscreenPageLimit() in onCreate() (after initializing ViewPager). The OffscreenPageLimit sets the number of pages that should be retained to either side of the current page (in your case 2). With this all of your fragments will be instantiate.
(An other (highly recommended) possibility is to increase the performance of your lists or listadapters, because a loading time of 2 seconds doesn't sound good)
Offtopic: thank you for your time and sorry for my English.
The problem is the lag when swiping on ViewPager.
The configuration used is a TabHost with Tabs (and dummy content), ViewPager with four fragments and FragmentPagerAdapter to manage it.
All 4 fragments have a list, some with other widgets in the layout. The thing is 2 fragments, or the 2 list in those fragments are quite heavy. The list items are LinearLayout with ~10 weighted items inside binded with a CursorAdapter. I know here resides the big problem, but I have struggled my small head to get an approach to this "percentage width" list item without any luck.
But, after some logs, I have figured out that the 3 managed fragments are "invalidated" during every swipe. I mean "invalidated" because what I see is every list binding items many ( a lot of ) times. And because of the redrawing of the 3 list, including my 2 heavy lists, the lag occurs.
2 things more to point of my code: I'm using cursor loaders, that I'm sure are not the cause of recreation because loader callbacks aren't arise on swipe.
On FragmentPagerAdapter.onPageSelected I'm only updating the current tab.
Setting ViewPager.setOffScreenPageLimit(3) to keep 4 fragments alive do things worse. Even when I swipe from fragment 3 to 4, the list on first fragment is re-rendered, what I really don't want and don't understand.
My workaround is using the onPageSelected to set the visibility of current list view to Visible and the others hidden. With these approach the lag disappears (let's say 95%), but I can see, when I scroll the empty list view before is created.
I would like to ask, if I'm wrong at some point, maybe missing an important idea. If anyone has a possible solution, maybe avoiding those recreations (I don't know how) or, if other people is getting same, and there is nothing to do to solve it.
I will try to take I look to ViewPager source, but I've just opened the file woouu..
I will try to get a test project with this issue.
Thank you very much.