Is there any way for me to invoke a callback when the user swipes from one pane to the next? As far as my experimentation has revealed, the present (primary) pane/fragment as well as the adjacent two (to the left and right) are all 'resumed', so none of the regular fragment lifecycle callbacks are invoked. I'm interested in this in order to hide a MediaController as soon as the pane it's associated with has been left.
You can attach an OnPageChangeListener to your ViewPager to get updates on when the user swipes to a new page:
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int state)
{
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
#Override
public void onPageSelected(int position)
{
Log.d("MainActivity", "New position = " + position);
}
});
And if you use a FragmentPagerAdapter, your ViewPager's pages will be cached in memory. If you use a FragmentStatePagerAdapter, they will not be cached.
the Fragment's setUserVisibleHint() would be callback as long as a fragment was completely disappeared or appeared, hide or show the MediaController operation can be done here.
public class YourFragment extend android.support.v4.app.Fragment {
...
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
System.out.println("VISIBLE");
} else {
System.out.println("GONE");
}
}
...
}
Related
I am stuck in this problem.
I want to maintain live instance of Fragment in application context. What I did is
1st Attempt
public class BaseProjectFragmentParent extends BaseProjectFragment {
#Override
public void onResume() {
super.onResume();
App.getInstance().setLiveFragment(this);
}
#Override
public void onPause() {
super.onPause();
App.getInstance().setLiveFragment(null);
}
}
and I extend all fragments by this base class. I expected live instance to be there in Application class.
Problem is in ViewPager, I have two Fragment in ViewPager where both Fragment's onResume() is called and live instance is set the second Fragment. I am working on some common code so I don't want to use getCurrentItem().
Second Attempt
Next I tried to get this Live Fragment by getSupportFragmentManager().getFragments()
isVisible() is true for both Fragments. (Not useful)
isResumed() is true for both Fragments. (Not useful)
You can use
setUserVisibleHint(boolean isVisibleToUser)
In fragments. This flag is set by the OS when a fragment becomes visible:
class MyFragment extends Fragment {
public MyFragment() {
setUserVisibleHint(false) //This is needed because by default its true
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser)
//true if fragment is visible
}
}
However this wont get called inside Viewpager. You have to add a listener to the viewpager and then set your active page manually:
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
// Check if this is the page you want.
}
});
How to call a method in a fragment when the tab is changed.
I tried using onPause() in fragment, but it is not getting invoked.
I am using ViewPager.
Thanks!
You can override setUserVisibleHint(boolean isVisibleToUser) method and use it for this purpose. isVisibleToUser will be true when the tab is changed and the fragment is selected and in other fragment which was previously visible, this method will be called with false.
EDIT:
setUserVisibleHint will be called multiple times. But in that case, you can rely on getActivity() not being null. i.e. if (isVisibleToUser && getActivity() != null)
Add ViewPager.OnPageChangeListener to ViewPager by:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
<method call>
}
}
So whenever user swipes through pages of ViewPager, you will get callback in onPageSelected() method with position of that page and there you can call fragment's method
You can use interfaces here.
Define your interface in Activity that contains view pager
public interface MyInterface{
void callMethod();
}
initialize interface and call like this
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
public void onPageSelected(int position) {
myInterface.callMethod();
}
});
And implement that method in your fragment and call your method in callback.
Myfragment extends Fragment implements MyInterface{
//
#Override
void callMethod(){
//call your method here
}
}
I have used viewpager with pagertabstrip
I have called webservice in onpageselected but while moving to category every category trying to load so the scrolling was not moving smoothly.Is there any way to achieve only load webservice when category moved?
You can use following listener in which there is a method called onPageSelected(int position) you can handle your fragment or web service calling based on the position.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
At the moment, i have a viewpager,when page scroll,i have load api for get new information.
But in the first load viewpager,progress loading display two twice because it load first page and next page.
I only want to load the first page in viewpager.
How must I do?
best way is to create one interface. and implement this interface to all fragment.
e.g.
public interface FragmentInterface {
void fragmentBecameVisible();
}
in ur viewpager activity put like this e.g
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
#Override
public void onPageSelected(int position){
FragmentInterface fragment = (FragmentInterface) mAdapter.instantiateItem(viewPager, position);
if (fragment != null) {
fragment.fragmentBecameVisible();
}
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2){
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
in your fragment like
public class TestFragmet extends Fragment implements FragmentInterface {
#Override
public void fragmentBecameVisible() {
// your task to execute
}
}
Put OffScreenPageLimit to ZERO. It won't load other fragments.
viewPager.setOffscreenPageLimit(0);
hi mate i have to know when a page is no longer the current.
i think that i have to use Listener, i see that exist:
#Override
public void onPageSelected(int position) {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
but there arent listener to know when page X is no longer active
I think you can use onPageScrolled method to understand that the active page was changed.
From the documentation:
This method will be invoked when the current page is scrolled, either as part of a
programmatically initiated smooth scroll or a user initiated touch scroll.
Therefore onPageScrolled method should do the trick.
Not sure what you mean by "no longer active" but the onPageSelected gives the new index so you could do something like this:
private currentPage = 0;
public void onPageSelected(int position) {
if(position != currentPage){ //page has changed to
currentPage = position;
}
}