android viewpager lags when swiping and loading data - android

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.

Related

Viewpager called multiple times

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

ViewPager refreshing itself

Am running in some weird problem. I have implemented ViewPagerTabStrip by following the tutorial Here
It's working fine and i have implemented Three Tabs.
On the first Page /tab , i am loading the data from server & displaying it in the ListView
The problem is, when i swipe to the Third Page & then i swipe back to Second page, the data of the first page is being loaded from the server again.( View of first tab is re-created.)
From the logs, it seems that this is the behavior of ViewPager & Adapter.
When we are on the first tab, it creates the view of second tab/ page as well in background & when we are on the Second tab, it was doing same for Third tab in background & upon reaching to third tab, it didn't created the view for it self(i.e third tab).
But when we swipe back to second tab, it re created the view of first tab as well.
How to solve this ? we can't afford to load the data from server again and again while swiping through the tabs.
I mean is there any way to stop the reload of data (or re-creation of view) while just swiping through tab pages? It should load the data when the whole fragment activity of View Pager is created and not at the time of swiping.
Any help on this please ?
Yeah, the ViewPager only keeps a certain number of fragments in memory before destroying them- this default number is usually two. Thus, what you have to do is tell the ViewPager to retain those fragments.
The simplest solution is to simply tell the ViewPager to keep three fragments in memory as follows:
viewPager.setOffscreenPageLimit(3);
Here are a few suggested answers on how to retain fragments (with more than just this method) and the reasoning behind the default behaviour as well as these solutions:
ViewPager and fragments — what's the right way to store fragment's state?
retain state of viewpager fragments android

Viewpager+FragmentpagerAdapet+Fragments+Listview = insanely slow app

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.

Preload fragment's view in ViewPager on first run

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)

Delay in ViewPager touch event

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.

Categories

Resources