Why TAB swipe reload content? - android

I have 4 tabs, first with grid view of photos and three without anything content, why when i swipe from first to three or four tab and return to first all my grid reload data from server, how to disable it?

Hi try to set your method setOffscreenPageLimit() of PageViewer
viewPage.setOffscreenPageLimit(3);
How setOffScreenPageLimit works:
When you use the default implementation of setOffscreenPageLimit() it is only loading the one fragment which is to the right of it. For eg. when you are on index 1, it has index 2 loaded in memory but not index 0, so swiping left will have to generate a new fragment from scratch.

Related

Android: Viewpager :: Swiping smoothly between tabs

My Activity has a viewpager along with a tablayout. There are three tabs. Now while swiping between the tabs, there is noticeable delay (around 300ms) while i try to very quickly swipe from, say, the first to third tab.
Now the viewpager contains three fragments. I am using a FragmentPagerAdapter to declare the viewpager tabs (as this gave a slight improvement compared to when i used FragmentStatePagerAdapter). Also I have set offScreenPageLimit for the viewpager to 2 (since there are 3 tabs).
Now to find out what was going wrong, I initialised empty fragments for the tabs and the swiping was fine and smooth as expected.
Next, I implemented the full functional code for the middle/2nd tab and left the other two as empty fragment.
Now this is where things get interesting, I was expecting a small delay while i swipe into the middle fragment(it contains nothing but a listview), when it creates it views and plugs the adapter into the listview with data. However, that was not the case. There was a noticeable delay while swiping out of the middle fragment!
Thus I have reason to believe, that while swiping out, where the fragment's view is being removed from screen, there is something I need to take care of which will make the transition smoother.

fragment automatically refreshed in viewpager

I have viewpager with 4 tabs, and using fragments in each tabs. And i have webview in each fragment. when I was in the first tab there are no mistakes, until the third tab. When i back to first tab, fragment refresh automatic. It make webview loadUrl() again. I don't want it. How to stop that refresh ?
Another explain:
I have 4 tabs. I call it, Tab AA, BB, CC, and DD with adjacent. When I in Tab AA and go to Tab BB, there are no mistakes. But when I in Tab AA go to CC and go back to tab AA, the fragment in tab AA refresh again. And i want to know explanation about lifecycle in viewpager ? Thanks..
According to http://developer.android.com/reference/android/support/v4/view/ViewPager.html
you can use setOffscreenPageLimit to set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
Please take a look at the ViewPager Documentation. The default behaviour is to retain 1 page either side of what's currently displayed. You can change it using setOffscreenPageLimit (int limit)
just use
viewPager.setOffscreenPageLimit(4);

Load custom position of page when create viewpager fragment

I got a problem with viewpager fragment.
Example: i got an array: 1-2-3-4-5-6.
I want when i create viewpager fragment, it will jump to fragment position 3.
Now the way viewpager do is: load all the list seriated like (1 and 2 and 3 and..), after that this set viewpager.setCurrentItem(3,false).
But with the way like that, it take more time when user click to open viewpager, is any way to do like:
When create viewpager, it will load fragment with position 3 first, after finish build UI of fragment(3), it will load another position of fragment, so when user click to open viewpager, they will not have waiting for viewpager create.
So please give me the ideal for that case.

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 number of visible tabs

I have a ViewPager with PagerTabStrip and three tabs,lets say "1","2","3" and the situation is this:
1*When first tab is selected,the visible selection is this:
1 2-the selector is under 1,tab 1 and tab 2 are visible
2*But when I select the second tab,the result is this :
1 2 3
-the selector is under 2,all tabs are visible(this is what i want)
3* When I have selected the third tab :
2 3
-the selector is under 3,tab 2 and 3 are visible
So I want when whichever tab is selected - all tabs to be visible(like 2*).So what is needed to do that?If you need the source code I could provide it,but it seems that this can be fixed with some property of the ViewPager or just to use another control?I just want to see all tabs and to choose from them,not to move from 1 to 2 and then to see 3...
So it seems that it can`t be solve this with setting the argument to zero : ViewPager.setOffscreenPageLimit(0) doesn't work as expected
Any idea?
ViewPager gets rid of views that are not in focus. When you select the tab 3, tab 1 is away because of this, and when you select 1, the same happens to 3.
There is a property as you say:
viewPager.setOffscreenPageLimit(int limit)
See this for more info: http://developer.android.com/intl/es/reference/android/support/v4/view/ViewPager.html
Be carefull on keep all views, because ViewPager does this to save memory.
Good luck.
After this suggestion :
If you don't need the animated movement between tabs, then you don't need a ViewPager. Just use a FrameLayout as a container, and swap the Tabs manually in a TabListener –
I have decided to use Fragments and I added ActionBar.Tab for each fragment with TabListener.And everything works fine for now.
Thaks to everybody!

Categories

Resources