An extended ViewPager that has the below features:
- allows its
Fragmentpages to get notified when they are actually visible/invisible to the user - supports multiple levels of
FragmentViewPagers - provides methods to control its paging
Maven:
<dependency>
<groupId>com.sbrukhanda.fragmentviewpager</groupId>
<artifactId>fragmentviewpager</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>or Gradle:
compile 'com.sbrukhanda.fragmentviewpager:fragmentviewpager:1.0.0'The major feature of the FragmentViewPager is that it allows its Fragment pages to get notified when they are actually visible/invisible to the user via its FragmentVisibilityListener interface. Essentially, Fragment.onResume() and Fragment.onPause() methods functionality is restored via the methods provided by the FragmentVisibilityListener interface. In order to start using FragmentViewPager, just follows this simple instructions:
-
Attach
FragmentViewPagerprogrammatically or via XML to anActivityorFragment, as you would with nativeViewPager. -
Set
FragmentViewPager's adapter. It should be an instance ofcom.sbrukhanda.fragmentviewpager.adapters.FragmentPagerAdapterorcom.sbrukhanda.fragmentviewpager.adapters.FragmentStatePagerAdapter, or else callbacks for the visibility state of Fragment pages wont work correctly. This classes are carbon copies of the repsective classes from the support library, with just a few tweaks that allowFragmentViewPagerto do its magic ;-) -
Override
Activity.onResumeFragments()and callFragmentViewPager.notifyPagerVisible():
private FragmentViewPager mFragmentsPager;
@Override
public void onResumeFragments() {
super.onResumeFragments();
mFragmentsPager.notifyPagerVisible();
...
}or Fragment.onResume() and call FragmentViewPager.notifyPagerVisible():
private FragmentViewPager mFragmentsPager;
@Override
public void onResume() {
super.onResume();
mFragmentsPager.notifyPagerVisible();
...
}- Override
onPause()of the hostingActivityorFragmentand callFragmentViewPager.notifyPagerInvisible():
private FragmentViewPager mFragmentsPager;
@Override
public void onPause() {
super.onPause();
mFragmentsPager.notifyPagerInvisible();
...
}- Implement
FragmentVisibilityListeneron allFragmentpages that you wish to receive callbacks for their visibility state.
You are ready to go :-)
Although, it's not a good idea to have multiple levels of ViewPager inside your views, sometimes theres no other way around it. FragmentViewPager supports an infinite amount of levels (i.e. a FragmentViewPager can have one or many nested FragmentViewPagers as its pages, while preserving its main functionality) by following this simple rule:
Call FragmentViewPager.notifyPagerVisible() inside FragmentVisibilityListener.onFragmentVisible() of your Fragment page that hosts the nested FragmentViewPager:
private FragmentViewPager mNestedFragmentsPager;
@Override
public void onFragmentVisible() {
mNestedFragmentsPager.notifyPagerVisible();
...
}and FragmentViewPager.notifyPagerInvisible() inside FragmentVisibilityListener.onFragmentInvisible() of your Fragment page that hosts the nested FragmentViewPager:
private FragmentViewPager mNestedFragmentsPager;
@Override
public void onFragmentInvisible() {
mNestedFragmentsPager.notifyPagerInvisible();
...
}FragmentViewPager's paging can be disabled, i.e. user swipe events will be ignored, but all other means of changing its pages will still function as expected. This functionality is exposed through FragmentViewPager.isPagingEnabled() and FragmentViewPager.setPagingEnabled().
If for some reason, you were unable to integrate FragmentViewPager into your project, I urge you to check the demo subproject accompanying FragmentViewPager for a working sample. Should that not help you, I will be glad to help you with your issue (I would advice searching issue tracker before opening a potentially duplicate issue).
Copyright 2016 Serhiy Brukhanda
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.