Viewpager+FragmentpagerAdapet+Fragments+Listview = insanely slow app - android

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.

Related

ViewPager Lag using FragmentStatePagerAdapter

I'm trying to build an application made of 10 Fragments using ViewPager along with FragmentStatePagerAdapter (and setOffScreenPageLimit(3)). There are 5 Fragment which are going to perform realtime updates modifying TextViews, Graphs and ProgressBars (so they're always performing actions on the MainUi thread). In this scenario, when I slide between Fragments the animation is very laggy (takes 1 to 2 seconds to change fragment when I see them for the first time, then it takes less but there are always lags).
What I'm asking is: how can I improve the smoothness of transactions?
Should I change my way to implement fragments (using FragmentManager instead of ViewPager), slow down the animation or perform some sort of operations before the sliding?
I have not so much experience in Android so any help is appreciated.
Eventually, I've found a solution consisting of the incrementation of setOffScreenPageLimit argument.

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.

Destroying Fragments

I have an android application where I followed http://developer.android.com/training/animation/screen-slide.html to setup.
However, I have quite a few fragments in the ViewPager (not at once) and I'd like to destroy them when I'm not on them. To put it into perspective, I have one fragment that gets created every time the ViewPager's getItem(int position) is called - which is around 365 times (one for each day of the year). All was good until I added an ImageView to one of the pages (12 in total at the end of it...) and now I'm running out of memory if I try view 3 of those page fragments.
My question is, how do I remove/destroy the fragment when its not the current page? I tried popping the BackStack of the FragmentManager, but that didn't work (it doesn't seem to have anything in the BackStack, but then again - I could have been calling it in the wrong place which was the getItem() function)
I'd provide code, but its quite a lot to look through for the important parts. It has the exact same structure as the Tutorial in the link above.
Thanks
If you get memory issue after adding imageViews to pager, why not focus on the memory aspects of the bitmap you construct for the imageView just added to your app?
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Note that pictures from device cameras can be very, very big and can cause memory issues very quickly and , IMO you should focus first on that.
IMO , the standard ViewPager and standard Adapter are OK at managing memory, even if you have lots of Fragments that you cycle through the pager.
And, its a little complex to take on the issue of explicitly destroying pages, reloading fragments in a ViewPager due to the amount of detail in collaboration among the pager and the adapter.
If you want to take on the complexity of the adapter and pager yourself, you will need to get into the source code for the pager and for the adapter you select:
* {#link android.support.v4.app.FragmentStatePagerAdapter},
* {#link android.support.v13.app.FragmentStatePagerAdapter};
As you can see from many posts on the topic of managing fragments in a ViewPager, its not enough to simply call 'destroyItem()' on the adapter or to simply remove a fragment from the ListArray bound to the adapter before calling notifyDataSetChanged() on the adapter. You also must know exactly how the operation of getItemPosition works along with the ViewPager in order to get the result you want.
It will probably take lots of time to work through the ViewPager approach .
Solve it if you can by first focus on the bitmaps.

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.

Increase Android ViewPager performance

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.

Categories

Resources