Increase Android ViewPager performance - android

I'm creating a android application with target on 2.2. I still want to use the ViewPager provided in the android.support-v4.jar.
I'm using a FragmentPagerAdapter in combination with Fragment to display content.
Each fragment contains a WebView, displaying some html data.
Scrolling a WebView up and down works great but the swiping between pages in the ViewPager behaves very jerky and not at all as smooth as scrolling the WebView.
Is there any way you can increase the scroll performance of the ViewPager, maybe something like ConvertView from BaseAdapter?
I'm guessing I could load the url in a background thread but from what I've heard that's not best practice.
Any suggestions would be great, thanks.

I realized you could use the
viewPager.setOffscreenPageLimit(items.size());
to keep all items preloaded in memory and avoid the "freeze/laggy" performance while scrolling,.

You should consider reusing the Fragments by providing some method to reuse existing fragments in the ViewPager and populate or refresh it's UI with the new data.

Related

screenshot of all Views in ViewPager

I have a ViewPager that consist of List of ScrollViews. I want to make a screenshot from all of the Scrollviews after the first load of the ViewPager.
After ViewPager is created, i am going through the Views and creating bitmaps, but i get a screenshot only for the first pages that are loaded offscreen.
If i use the command:
pager.setOffscreenPageLimit(totalPages);
than I am able to get all of the screenshots. However this approach is memory consuming and my app crashes with outOfMemory when the ViewPager consists of a lot of pages.
Is there a way to create screenshot of all the views even the ones that are not preloaded - offScreen?
If you don't set offScreenPageLimit(totalPages), ViewPager will initially only load 1 more page other than you see. That means other pages will not be even created until you swipe there. That also means you cannot take screenshot of a screen/layout that is not created/inflated yet. And yes, it is not a good practice memorywise to set offscreen page limit to a number that ViewPager loads all pages at start.
Answer: No, if you mind your memory consumption. Otherwise, yes.

android -Need some suggestion for making a viewpager for very big datas

I want to make a leitner application . the data is coming from database .
For instance , I've about 500 rows in my database and I want to make a viewpager for showing them , each row comes on one page so I'll have 500 pages in the viewPager .
The question is , What is the best way to implement something like this ? is it going to have memory problems or something ?
What are the better ways to do so ? if no other way ,What type of viewPager is better to use for database? A fragment viewAdapter for example
For this you should use a normal ViewPager with FragmentStatePagerAdapter.
As described in documentation
This version of the pager is more useful when there are a large number
of pages, working more like a list view. When pages are not visible to
the user, their entire fragment may be destroyed, only keeping the
saved state of that fragment. This allows the pager to hold on to much
less memory associated with each visited page as compared to
FragmentPagerAdapter at the cost of potentially more overhead when
switching between pages.

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.

Preloading ViewPager Pages - because of lag

Is this possible to preload Fragments for ViewPager so that there's no lag when user swipes to next page? I've been trying two kinds of adapters but there's no use in switching from Fragmentstatepageradapter (I am using fragments to generate N pages) to fragmentpageadapter. I've got a lot of bitmaps to load in my pages. Setting viewpager offscreen pages doesn't help. Is there a way to preload them and inflate in backgdound? I know this is a silly question but I am a bit desperate about performance. Basically in my app when user scrolls to next page there is kind of lag, just between pages, and it's so annoying... Please help or point the right direction. I am targeting Gingerbread so I need to use that what Android Support gives.
Is this possible to preload Fragments for ViewPager so that there's no lag when user swipes to next page?
Sure. Have your adapter preload the fragments. You are responsible for returning fragments in getItem() -- whether those are "preloaded" or not is your job.
That being said, your problem most likely is not with the fragments themselves, but something that the fragments are doing. For example, you "got a lot of bitmaps to load in my pages", and if you are doing work related to those on the main application thread (e.g., using BitmapFactory), you are causing your own lag.
So, instead of racing off to "preload" stuff, a talented programmer would find out specifically what is causing the lag, using tools like Traceview. Then, and only then, would a talented programmer start working on a fix.

Pattern for using dynamic loaders to load ListView pages in a ViewPager without ListFragments?

If I have a ViewPager with a certain number of pages, what would be the best way of loading the data for each page using the Loader framework? Each page contains a ListView with its own adapter instance.
Use a separate loader for each page; or
Use a single loader that checks which pages are not loaded and loads them
The thing is, it's possible the user may want to quickly swipe through a bunch of pages and thus it should be easy to "cancel" loading pages that aren't needed for performance reasons.
On the other hand it seems like there might be an overhead with having a lot of loaders.
EDIT: My Loaders actually need to have dynamic IDs to represent the content they are loading. I'm probably going to go with using a mask on the loader ID, and then once the loader is done call destroyLoader().
I would use a separate Loader for each ListFragment. Things will get messy very quickly if you try to manage the fragments' Loaders from the parent Activity (especially if those fragments are being held by a ViewPager). Fragments are supposed to be designed for reuse and should not rely on a specific Activity in order to function properly. The fact that each Activity and each Fragment get their own LoaderManager instance is further evidence that the Android team didn't want developers to have their Activitys loading data for its attached Fragments.
I don't think there will be as much overhead as you are probably expecting there to be... Loaders should retain their data across the component's lifecycle and re-query only when the underlying content has changed. It might be a good idea to use a FragmentStatePagerAdapter too.

Categories

Resources