ViewPager2自动滚动,直到适配器itemCount结束

问题描述 投票:0回答:1

我已经尝试了几种不同的方法,但我无法使此viewpager正确执行。我正在使用适配器设置viewpager2,但是部分要求是viewpager可以手动滑动,也可以基于按钮来增量以增加分页器视图。我已经滑动并单击了按钮,才按预期的方式移动视图寻呼机,但是自动滚动是有问题的。

我正在viewPager上设置postDelayed可运行。刚完成的日志在运行时很有意义,您可以在下面看到输出的样子。

companion object {
    private const val TAG = "LauncherActivity"
    private const val timerDelay: Long = 10 * 1000 // 1 minute in milliseconds
    var position: Int = 0
}
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    ...
    pager.autoScroll(timerDelay)
    ...
    pager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            Log.d(TAG, "-> OnPageChangeCallback() -> position: ${position}")
            super.onPageSelected(position)
            if (position != LauncherActivity.position) LauncherActivity.position = position
        }
    })
}

fun ViewPager2.autoScroll(interval: Long) {

    val count = adapter?.itemCount ?: 0

    val handler = Handler()
    val runnable = object: Runnable {
        override fun run() {
            if (position < count) {
                Log.d(TAG, "Autoscroll: current position = ${position} ending position = ${position + 1}")
                setCurrentItem((position++ % count), true)
                Log.d(TAG, "New position: ${position}")
                handler.postDelayed(this, interval)
            }
        }
    }
    handler.post(runnable)
}

更好地解释发生了什么的日志

2020-05-22 11:42:51.485 D/LauncherActivity: Autoscroll: current position = 0 ending position = 1
2020-05-22 11:42:51.485 D/LauncherActivity: New position: 1
2020-05-22 11:42:51.621 D/LauncherActivity: -> OnPageChangeCallback() -> position: 0
This is initial load. The view is still showing position 0 in viewpager. Seems like the postDelayed ran but didnt run?

2020-05-22 11:43:01.492 D/LauncherActivity: Autoscroll: current position = 0 ending position = 1
2020-05-22 11:43:01.492 D/LauncherActivity: New position: 1
10 seconds later the post delayed runs again but doesnt actually change the viewpager. BUT it starts
everything off as it should. lack of OnPageChangeCallback() indicated it didnt change the view.

2020-05-22 11:43:11.497 D/LauncherActivity: Autoscroll: current position = 1 ending position = 2
2020-05-22 11:43:11.503 D/LauncherActivity: -> OnPageChangeCallback() -> position: 1
2020-05-22 11:43:11.506 D/LauncherActivity: New position: 1
The view finally changed to the next position

2020-05-22 11:43:21.518 D/LauncherActivity: Autoscroll: current position = 1 ending position = 2
2020-05-22 11:43:21.519 D/LauncherActivity: New position: 2
10 seconds later the post delayed runs again but doesnt actually change the viewpager.

2020-05-22 11:43:31.532 D/LauncherActivity: Autoscroll: current position = 2 ending position = 3
2020-05-22 11:43:31.534 D/LauncherActivity: -> OnPageChangeCallback() -> position: 2
2020-05-22 11:43:31.535 D/LauncherActivity: New position: 2
Finally it has changed to the final item.

2020-05-22 11:43:41.548 D/LauncherActivity: Autoscroll: current position = 2 ending position = 3
2020-05-22 11:43:41.550 D/LauncherActivity: New position: 3
Not a clue why it ran again...
android runnable autoscroll android-viewpager2 postdelayed
1个回答
0
投票

我会删除autoScroll(Long)方法并像这样:

  1. 在registerOnPageChangeCallback的onPageSelected()]的末尾添加:
  2. handler.postDelayed(pager.setCurrentItem(position ++),timerDelay)

  1. onCreate()]末尾启动初始页面更改)

    这样,您将开始滚动项目的循环。

    pager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            Log.d(TAG, "-> OnPageChangeCallback() -> position: ${position}")
            super.onPageSelected(position)
            if (position != LauncherActivity.position) LauncherActivity.position = position
            handler.postDelayed(pager.setCurrentItem(position++), timerDelay) // repeat
        }
    })
    pager.setCurrentItem(position++) // Start the autoscroll
    // or like this if You want to give it some time
    // handler.postDelayed(pager.setCurrentItem(position++), timerDelay)
    
  2. 而且我也不知道为什么在[]中使用remainder

setCurrentItem((position ++%count)

感觉它不应该在那里。

© www.soinside.com 2019 - 2024. All rights reserved.