gpt4 book ai didi

android - 在 ViewPager2 android 中检测过度滚动或页面结束事件

转载 作者:行者123 更新时间:2023-12-02 16:27:03 26 4
gpt4 key购买 nike

ViewPager2 中没有过度滚动的方法,我需要检测过度滚动,以便在没有更多页面时将用户带回主页。

最佳答案

ViewPager2 没有过度滚动的公共(public)方法,但是,可以通过监听onPageScrollStateChanged

来检测

通常 onPageScrollStateChanged 中滚动状态的事件转换是 SCROLL_STATE_DRAGGING -> SCROLL_STATE_SETTLING -> SCROLL_STATE_IDLE 当改变时一页

但在过度滚动的情况下,序列是 SCROLL_STATE_DRAGGING -> SCROLL_STATE_IDLE

private fun listenOverScroll(currentIndex: Int, size: Int) {
var index = currentIndex
var previousState = ViewPager2.SCROLL_STATE_IDLE
viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {

override fun onPageSelected(position: Int) {
super.onPageSelected(position)
index = position
}

override fun onPageScrollStateChanged(state: Int) {
Log.d(TAG,"Index:: $index | state:: $state | prevState:: $previousState")
super.onPageScrollStateChanged(state)
if ((index >= size - 1 || index <= 0)// end of list. these checks can be
// used individualy to detect end or start of pages
&& previousState == ViewPager2.SCROLL_STATE_DRAGGING // from DRAGGING
&& state == ViewPager2.SCROLL_STATE_IDLE) { // to IDLE
Log.d(TAG,"OVERSCROLL:: Index:: $index | state:: $state | prevState:: $previousState")
//overscroll performed. do your work here
}
previousState = state
}
})
}

关于android - 在 ViewPager2 android 中检测过度滚动或页面结束事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64240817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com