A ViewPager that supports infinite looping effect, smart auto-scroll, compatible with any indicators and easy to use. It especially uses it as banners of application with a simple item page.
Looping Viewpager can:
- Plug and play, easy to use
- Infinite Looping items
- Auto scroll items, allow config, auto resume/pause when activity/fragment resume/pause
- Won't scroll or loop if it has only 1 item
- Compatible with many indicators
Simple pager | Pager with indicators |
You can download the apk for the sample app of this library at this link.
The code of the sample app is available at this link.
Having the sample apps installed is a good way to be notified of new releases. Although watching this repository will allow GitHub to email you whenever a new release is published.
The Gradle dependency is available via MavenCentral.
The minimum API level supported by this library is API 16.
The latest version here [Download]
dependencies {
implementation("io.github.kenilt:loopingviewpager:1.0.0")
}
Just replace your ViewPager with LoopingViewPager and use it as normal
// Just set adapter and indicator as a normal view pager
vpPager.adapter = adapter
indicator.setViewPager(vpPager)
<com.kenilt.loopingviewpager.widget.LoopingViewPager
android:id="@+id/vpPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"/>
// set adapter for view pager
vpPager.adapter = adapter
// when your adapter is FragmentPagerAdapter or FragmentStatePagerAdapter,
// use this function for better performance
vpPager.setAdapter(adapter, supportFragmentManager)
// set view pager for indicator
indicator.setViewPager(vpPager)
// active auto scroll
val autoScroller = AutoScroller(vpPager, lifecycle, 3000)
// pause auto scroll
autoScroller.isAutoScroll = false
If your adapter is instance of FragmentPagerAdapter or FragmentStatePagerAdapter, you should to use vpPager.setAdapter(adapter, supportFragmentManager)
to get better performance, if not, LoopingViewPager will use reflection to get fragmentManager from your adapter to do its job.
By default, LoopingViewPager will force use behavior of FragmentPagerAdapter and FragmentStatePager adapter to BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
, because BEHAVIOR_SET_USER_VISIBLE_HINT
is deprecated now.
AutoScroller is a separate class, so if you want your pager/banners auto-scroll, just create an AutoScroller(vpPager)
and it will automatically scroll your pager/banners every scrollInterval
(default is 5000 milliseconds).
If you want AutoScroller
auto resume when Activity/Fragment resumed, and auto pause when Activity/Fragment paused, pass lifecyle
as a parameter when created it AutoScroller(vpPager, lifecycle)
.
To change interval time, just call autoScroller.scrollInterval = value
.
To resume/pause auto scroller, just call autoScroller.isAutoScroll = true/false
- Thanks Pierfrancesco Soffritti - Author of android-youtube-player - for awesome sample app library and lifecyle idea.
- Thanks SanyuZhang - Author of CircularViewPager - for awesome internal adapter and listener idea.
- Thanks Sira - Author of LoopingViewPager - for awesome library and auto scroll idea.
- Thanks TobiasBuchholz - Author of CircularViewPager - for looping idea.
The MIT License
Copyright (c) 2020 Kenilt Nguyen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.