Fragment ViewPager


Source link: https://github.com/sbrukhanda/fragmentviewpager

Fragment ViewPager

An extended ViewPager that has the below features:

  • allows its Fragment pages to get notified when they are actually visible/invisible to the user
  • supports multiple levels of FragmentViewPagers
  • provides methods to control its paging

Download

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'

Basic usage

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:

  1. Attach FragmentViewPager programmatically or via XML to an Activity or Fragment, as you would with native ViewPager.

  2. Set FragmentViewPager's adapter. It should be an instance of com.sbrukhanda.fragmentviewpager.adapters.FragmentPagerAdapter or com.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 allow FragmentViewPager to do its magic ;-)

  3. Override Activity.onResumeFragments() and call FragmentViewPager.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();

  ... 
}
  1. Override onPause() of the hosting Activity or Fragment and call FragmentViewPager.notifyPagerInvisible():
private FragmentViewPager mFragmentsPager;  @Override public void onPause() {

  super.onPause();

  mFragmentsPager.notifyPagerInvisible();

  ... 
}
  1. Implement FragmentVisibilityListener on all Fragment pages that you wish to receive callbacks for their visibility state.

You are ready to go :-)

Nested FragmentViewPager

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();

  ... 
}

Controlling paging

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().

Having trouble using FragmentViewPager ?

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).

License

  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. 

Resources

A simple library based on PopupWindow for creating Tooltips on Android.

An Android task queue library. Support priority, timeout, multiple queue and auto-retry.

Save time & clear your unit tests on Android!

Use annotations to inject Context, Activities, Fragments and Views into your tests.

Android EditText view with tags.

A simple library to build a simple walkthrough activity.

ScratchView repo is UX Design involving scratch cards like views which are scratched to reveal the information they conceal.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes