Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class LoopingViewPager : ViewPager {
private var originalAdapter: PagerAdapter? = null
private var internalAdapter: PagerAdapter? = null
private val pageChangeListenerMap = HashMap<OnPageChangeListener, InternalOnPageChangeListener>()
private var insideArrowScroll: Boolean = false

constructor(context: Context) : super(context)

Expand Down Expand Up @@ -100,11 +101,16 @@ class LoopingViewPager : ViewPager {
}

override fun setCurrentItem(item: Int, smoothScroll: Boolean) {
// setCurrentItem() assumes it is passed an item position from the
// original adapter, however when it is invoked from within arrowScroll()
// it is passed an item position from the internal adapter, so the code
// should account for it.
val originalItem = if (insideArrowScroll) getPagerPosition(originalAdapter, item) else item
val count = adapter?.count ?: 0
if (item == 0 && currentItem == count - 1) {
if (originalItem == 0 && currentItem == count - 1) {
super.setCurrentItem(count + 2, smoothScroll)
} else {
super.setCurrentItem(getInternalPosition(originalAdapter, item), smoothScroll)
super.setCurrentItem(getInternalPosition(originalAdapter, originalItem), smoothScroll)
}
}

Expand All @@ -123,6 +129,13 @@ class LoopingViewPager : ViewPager {
}
}

override fun arrowScroll(direction: Int): Boolean {
insideArrowScroll = true
val handled = super.arrowScroll(direction)
insideArrowScroll = false
return handled
}

private fun handleSetCurrentItem(position: Int) {
val itemCount = internalAdapter?.count ?: 0
if (itemCount <= 1) return
Expand Down