You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
I think I am not maintaining version 1.x.x anymore, and if I have time and interested in the future, I might re-write it using Compose.
With that said, I do think v1.4.1 is stable enough to use with ViewPager (Not ViewPager2). In case it is having issue with new versions of Kotlin / Android libraries, I wish you can folk it and use it with simple fixes. Thank you!
LoopingViewPager
A ViewPager and a PagerAdapter that can:
AutoScroll (On/Off able)
Infinite Loop (On/Off able)
Adjustable auto scroll interval
Won't scroll nor loop if there is only 1 item
Works well with notifyDataSetChanged()
Supports page indicators
Supports different view types
Support peeking adjacent items (But first and last item will appear only after scroll state is idle)
Demo Effect
Why this library
Although there are already quite a number of similar libraries out there, I cannot find one that fits all of the below requirements:
Sufficient documentation
Last updated in less than 3 years
Good infinite looping effect
Configurable auto-scroll
Good support with Page Indicators
Especially for 5, even some of them supports, they provide built-in indicators only; or does not tell user how to implement their own indicator. I wrote this library to tackle all of these problems I faced after trying a whole day with other libraries.
Usage
Add to Project
First make suremavenCentral() is included as a repository in yourproject's build.gradle:
allprojects { repositories { mavenCentral() }}
And then add the below to your app's build.gradle:
(The above xml example is placed inside a ConstraintLayout.)
Attribute Name
Default
Allowed Values
isInfinite
false
true / false
autoScroll
false
true / false
scrollInterval
5000
any integer (represents ms)
Please note that the height ofLoopingViewPager should be decided by its parent, like all other Views. If you want it to have a specific aspect ratio, you can place it inside aConstraintLayout and give it the attributelayout_constraintDimensionRatio.
(The old versions of this library uses an internal attribute to determine its height, which is completely wrong and can lead to bugs!)
Note: If you want the ViewPager to be able to peek adjacent items, make use ofclipToPadding=false and set padding to the ViewPager. (itemAspectRatio has been removed in v1.4.1)
Step 2: Create your PagerAdapter that extends LoopingPagerAdapter
You should
Specify the data type in the generic (<DataType>)
Create your own constructor according to thisDataType
overrideinflateView() andbindView()
DO NOT override getCount(). Look atLoopingPagerAdapter. getCount() has special implementation.
classDemoInfiniteAdapter(itemList:ArrayList<YourObject>,isInfinite:Boolean) : LoopingPagerAdapter<YourObject>(itemList, isInfinite) {//This method will be triggered if the item View has not been inflated before.overridefuninflateView(viewType:Int,container:ViewGroup,listPosition:Int ):View {returnLayoutInflater.from(container.context).inflate(R.layout.item_pager, container,false) }//Bind your data with your item View here.//Below is just an example in the demo app.//You can assume convertView will not be null here.//You may also consider using a ViewHolder pattern.overridefunbindView(convertView:View,listPosition:Int,viewType:Int ) { convertView.findViewById<View>(R.id.image).setBackgroundColor(convertView.context.resources.getColor(getBackgroundColor(listPosition)))val description= convertView.findViewById<TextView>(R.id.description) description.text= itemList?.get(listPosition).toString() }}
And then, of course, according to theviewtype parameter passed to you ininflateView() andbindView(), differentiate what you need to inflate or bind.
You may also refer to the demo app for a complete example.
How do I integrate a Page Indicator?
I don't officially provide a built-in page indicator because:
ViewPager and Indicator are logically separated
I want to make this library adaptable to all page indicators
With that said, I have created a simpleCustomShapePagerIndicator included in this library. It is simple but very flexible, because you need to / you can provide your own implementation of how to inflate the unselected and selected indicators.
I assume for any indicator libraries you use, they should provide a method for you to call so that you can simply redirectonPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) ofViewPager.OnPageChangeListener to the indicator library, And then the indicator library will then be able to handle the transitions for you, which is also the case inCustomShapePagerIndicator.
So, the job ofLoopingViewPager is simply to mask the infinite behavior into aonPageScrolled behavior which looks like it is not infinite. In other words, when you scroll from last page to first page, it will be converted to directing jumping from last page back to first page; When you scroll from first page backward to last page, it will be converted to directing jumping from first page to last page. If you do not accept this assumption, I am sorry but you probably need to find something else.
So, instead of adding anotheronPageChangeListener, you simply do this: (Below example is using built inCustomShapePagerIndicator)
Note that this is a brand new implementation of howLoopingViewPager in v1.3.0, after I realized how wrong I was 2 years ago. If you need to stick with the old implementation, please use v1.2.0 and checkout the README.md at the commit before this version.
If you want to use CustomShapePagerIndicator, here is how
(You can always refer to the demo app.)
First, addCustomShapePagerIndicator to your XML, use wrap_content for width and height, And specify the spacing between unselected indicators (app:indicator_spacing="?dp"):
Note that you must provide a fixed width and height for your indicators. BecauseCustomShapePagerIndicator use margins internally to handle indicator spacing, please do not specify your own margin when providing your own views.
Finally, for the first time and whenever you need to update your indicator counts:
Removed setting aspect ratio inside LoopingViewPager. Let its parent to decide how to layout it (i.e. its height).
Same as above, removed itemAspectRatio as well. You can actually simply set it through padding andclipToPadding=false.
Removed unnecessary context as a param in LoopingPagerAdapter's constructor.
Migrated from JCenter() to MavenCentral().
v1.3.2
Fixed crash due to getChildAt() returns null
v1.3.1
Converted everything to Kotlin
Revamp the whole logic of pager indicator handling
Added built-inCustomShapePagerIndicator
v.1.2.0
Added requirement mentioned in #17.
v1.1.4
Merged #11.
v1.1.3
Merged #15 which includes AndroidX support andresetAutoScroll() which is equivalent topauseAutoScroll() and thenresumeAutoScroll().
v1.1.2
AddedViewGroup container as an argument toinflateView(). You should now use it as the parent of when you inflate your view.
Updated gradle version and support library version.
v1.1.0
Added support for view type. But therefore changed parameters needed ininflateView() andbindView().
v1.0.5
Added asepct ratio attribute forLoopingViewPager
Rewrote the way of caching Views inLoopingPagerAdapter, and therefore separated inflation and data binding
Rewrote the way of implementing ViewPager wrap_content
v1.0.4
Indicator now works with page skipping as well (By callingselectCurrentItem())
Leviated indicator fluctuating phenomenon when usingonIndicatorProgress() callback
Added option for smart or non-smart indicators
v1.0.1
Fixed a bug where getSelectingIndicatorPosition() is returning incorrect value.
Updated README.md according to PageIndicatorView v1.0.0 update.
License
Copyright 2017 Sira LamPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the LoopingViewPager), 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 PURPOSEAND 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.
About
A ViewPager and PagerAdapter combination that support auto scroll, infinite loop and page indicators.