Stop preloading pages in viewpager example - android

I am looking for and example source code for stop pre-loading pages in viewpager. Normally android viewpager loads the next and previous page(s) automatically. My target is to use viewpager and I want to show animation on views(which are present in the pages) when it visible to the user. Basically it is to continue with the previous of post.
If you can give me any working example(complete code) it will be so kind. I found so many but did not find any working implementation.
Thanks for your help.
Regards
Biswajit

Like you already know there is no way of stopping viewpagers from loading . The only way out is to use this method setUserVisibleHint add this to your fragment, and do all the animation you want to in this method
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// animate here
Log.e(TAG, "animate here");
}else{
Log.e(TAG, "fragment is no longer visible");
// fragment is no longer visible
}
}
This will be called only when that particular tab is visible to user.

Related

How to restrict to view pager to not execute any code from other fragment

I have a view pager which contains 2 fragment.
Now when user loads the activity, both of view gets loaded, they both contain analytics tag, and apps sends view tag from both of the fragments.
I want to restrict this. I want it to not execute any code from fragment2 unless user taps on it from view pager tab.
In the case of ViewPager, it is not possible I think...the minimum offScreenPageLimit is default set to 1. So it will load one page each side by default. So better you make it a custom view and load fragment according to the user taps.
Or check this workaround maybe this can be helpful in your case. I have never tried it but you can give it a shot
you can do the stuff after fragment gets visible
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
}
else {
}
}
The best implementation of a viewpager is to recycle the viewpager items. And you can achieve it by using offScreenPageLimit.
However, answering your question, i have faced similar situation myself and there is no perfect solution yet to achieve such a scenario. But there are certain hacks that you can do.
One solution is to use
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
}
else {
}
}
This will execute only when fragment is visible to user.
But this function too has some pitfalls. In some edge cases, this doesn't execute as well which will lead to an empty fragment.
The hack that I do is to use "OnTabSelectedListener" if I am integrating viewpager with tablayout and if there is only Viewpager you can override "OnPageSelectedListener".
If you are using OnTabSelectedListener, you will get callbacks in "OnTabSelected()" method when a tab is selected. So when you get the callback, use a public function in the respective fragment to load the data(Api calls, setting adapter etc).
Hope this helps

Fragment lifecycles in ViewPager

There is a ViewPager with Fragments generated dynamically.
Questions:
What is the way to catch the moment when user slides away from the fragment (so I can bring it into "clean", "init" state)?
or
How to catch moment when a Fragment is scrolled in?
Problems:
Have checked Fragment Lifecycle, but none of them is getting triggered when scrolled out/in (using ViewPager)
Lifecycle phases are triggered only if I scroll 2+ Fragments (those 3rd one is getting Paused/Resumed).
To get a callback when a fragment gets visible to the user you can override the setUserVisibleHint method, like this:
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
//Put your 'init' logic here
}
}
the variable isVisibleToUser will give the status of the visibility, so you can use the same method to handle when the fragment goes out.
addOnPageChangeListener
void addOnPageChangeListener (ViewPager.OnPageChangeListener listener)
Add a listener that will be invoked whenever the page changes or is incrementally scrolled. See ViewPager.OnPageChangeListener.
Components that add a listener should take care to remove it when finished. Other components that take ownership of a view may call clearOnPageChangeListeners() to remove all attached listeners.
You can implement this listener to track the movement of the fragments in the ViewPager.
There is also a method setOffscreenPageLimit() which Set the number of pages that should be retained to either side of the current page and it's default value is 1 and minimum value can be set to 0.

I want to disable preloading next page in a ViewPager

How can I disable the preloading in a ViewPager?
I have a ViewPager with 3 pages fragment in each page. So I don't want to load the next page and previous page. How can I disable this behaviour?
I tried
viewPager.setOffscreenPageLimit(1)
But this is not working.
and tried
#Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
// do something nice here
}
}
but RecyclerView which in next fragment show this
RecyclerView: No adapter attached; skipping layout
apologize for my English
The minimum value for the off screen limit is 1.
what you can do is, handle it yourself using the "setUserVisibleHint" method in your fragments, like for instance.
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser)
// execute your data loading logic.
}
I think its not possible with ViewPager. To swipe between two pages both the pages should be visible. (Should be preloaded). You can try your own view pager to swipe without preload. Also Check This
Here is an example Android application that extends the Fragment Life Cycle with two new methods: onResumeViewPage() and onPauseViewPage() that are called when the user swipes between fragments. If you put your code in onResumeViewPage() it should work.
See: Slider Android Application in BitBucket.

Android - how to refresh tab contents

I have two tabs with fragments created in my activity, the data of the second tab depends on the first tab data.
Both the tabs are containing list items, when I delete an item from first tab's list, it gets deleted but the second tab still shows the data. I need to go back from activity and load again to see the updated data in second tab.
Is there any way that I can refresh the second tab content whenever I delete/update list item of first tab?
Note: I have tried -
detach()
attach()
and
runQueryOnBackgroundThread()
notifyDataSetChanged()
In onResume nothing seems to be working. Any help would help a lot
You should learn about Interface communication. Read this http://developer.android.com/training/basics/fragments/communicating.html will help you.
you should override setUserVisibleHint method in your fragment and put your refreshing code in it
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && isResumed()) {
}
}
I got this code in help.Hope it will help more people.Or refer my question here
http://stackoverflow.com/questions/40505019/how-to-refresh-tabs-content-dynamically-while-switching-between-one-tab-to-other/40505242#40505242

Detect ViewPager tab change inside Fragment

I have a ViewPager with multiple fragments. In one Fragment I play audio. When I swipe to another fragment I want to stop the audio playback. How do I detect that the another fragment is now visible in the ViewPager?
I've tried overriding onStop and onHiddenChanged. No success. There must be some "you're not active anymore" method to override. No?
I did some digging and it turns out that ViewPager will call both: setUserVisibleHint and setMenuVisibility. I would override setUserVisibleHint since the documentation for setUserVisibleHint states:
Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore.
An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user. This may be used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior.
Try putting this code in your fragment:
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// Make sure that we are currently visible
if (this.isVisible()) {
// If we are becoming invisible, then...
if (!isVisibleToUser) {
Log.d("MyFragment", "Not visible anymore. Stopping audio.");
// TODO stop audio playback
}
}
}

Categories

Resources