Android ViewPager Gallery - android

I am using a view pager to show a list of images, it works fine but the main issue is that when I swipe between images, there is some kind of fading between images.
for example if I have 5 pictures and the first one is visible when i swipe the second one is visible directly.
if i decided to swipe faster, it shows white screen then the image appears "moving from first to third quickly - as example".
I think the fragment is being recycled - is there any way to avoid this ??

It's hard to tell with no details on your implementation, but just to answer your last question (I'm not sure if it will necessarily solve your problem) - if you are using a fragments in your ViewPager and are currently using a FragmentStatePagerAdapter, you can switch to a FragmentPagerAdapter, which will keep the fragments in memory (although it will of course use more memory, so you might not want to do it if you have a large number of fragments in your ViewPager). You can also specify how many offscreen pages for the ViewPager to keep using
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);
In the above case it would keep 2 pages on each side of the currently viewed page. Sorry, I would have confirmed the specific problem with you before posting this answer, but I don't have the status yet :(

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.

ViewPager FragmentStatePagerAdapter keep more than three fragments in memory

The problem is ive got sort of book reader, landscape shows two pages and portrait just one. For some reason I blocked destroying activities on config change. the story is pretty much the same like here ViewPager + FragmentStatePagerAdapter + orientation change. Although the question is bit different - Id wish to store not 3 but 5 fragments. its about rendering time, Im easily handling swipe stuffs with adding +-1 in portrait and +-2 in landscape, though the problem is, in landscape orientation, it wont render proper next / previous fragments. As obviously its rendering +1 and -1 items, I want +2/-2 items to be instantiated instead. So theres either some kind of solution that changes positions to instantiate in landscape (which im not aware of though), or storing 5 items in an adapter instead of just 3.
Anyone with any sort of proper solution?
You can store 5 items in the adapter by setting the OffscreenPageLimit of the ViewPager to 4:
viewPager.setOffscreenPageLimit(4);
hope that I've helped you.
try this:
mViewPager.setOffscreenPageLimit(4);

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.

ActionBarSherlock + ViewPager caching more then just prev/next view

on the page:
onCreate called for two tabs each time one tab is selected
there is explained how the ABS(actually ViewPager) is working in order for ViewPager to be able to do a scrolling.
It is clear that at least a prev/next page need all to be created at the same time.
Would it be possible to "cache" more than just prev/next Views(Fragments), in a way:
I am on Page 1 and there I have a network call to fetch some data(doing this in Activity, not in Fragment - btw. is this OK?)
switch to Page 2, and then
switch to Page 3, and then
switch to Page 1 = Here my page is recreated (using some caching though, BUT, I do not need any recreation if possible)
So, it would be nice to cache all the pages. How to accomplish this If possible in current version (4), or this would be some new feature?
Or even better question, how to postpond/disable destroying of views?
I suppose It might be useful to someone else, as pointed on website
http://storkme.org/2011/12/tabs-done-right-viewpager-and-fragments/
There is already a method which allows you to predefine number of "cached" pages the ViewPager will save from recreating.
mViewPager.setOffscreenPageLimit(3);
Thanks Jake for reminding me to do a different G search :)

Android Viewpager with asynctasks (spinner while page is loading)

I want a Viewpager that shows loading while content is coming in from the background. Basically I expect the first View to be loaded, but View+1 and View-1 will still be loading. If the user swipes to either side I want them to be presented with a spinning dialog while it loads
Would I just add AsyncTasks into the ViewPager with some conditions determining when they will run? I dont want too many AsyncTasks to be loading as the viewpager will have many views off to the sides.
I think the Trulia app does this, it is similar to what I am looking for. Apartment image viewing shows a loading screen while the images are loading in that viewpage.
Also for the record, can I just treat viewpagers like onCreate functions of an activity? That would really clear things up
Insight appreciated
Have a look at the supplied FragmentPagerAdapter if you want to perform more Activity-like lifecycle management of each page.

Categories

Resources