英文:
Is it possible to set first coming page of viewPager2 with Intent in Kotlin?
问题
我在Kotlin中使用ViewPager2。
根据数据库的数据值,我像下面这样更改viewPager的页面索引:
在DiaryActivity中:
diaryDB
    .document("${userId}_${writeTime}")
    .get()
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val document = task.result
            if (document != null) {
                if (document.exists()) {
                    viewPager.currentItem = 1
                } else {
                    viewPager.currentItem = 0
                }
            }
        } else {
            viewPager.currentItem = 0
        }
    }
但是当我通过Intent从其他Activity调用DiaryActivity时(如果数据值需要跳转到currentItem 1),它首先跳转到currentItem = 0,然后再滑动到currentItem = 1。
我不希望屏幕首先显示0,然后再滑动到1,我希望屏幕首先显示1。
使用ViewPager2是否有可能实现这个效果?
英文:
I use ViewPager2 in Kotlin.
And according to the data value of DB, I change viewPager's page index like below:
in DiaryActivity:
        diaryDB
            .document("${userId}_${writeTime}")
            .get()
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val document = task.result
                    if(document != null) {
                        if (document.exists()) {
                            viewPager.currentItem = 1
                        } else {
                            viewPager.currentItem = 0
                        }
                    }
                } else {
                    viewPager.currentItem = 0
                }
            }
But When I call DiaryActivity from other Activity by Intent, (If data value needs to go currentItem 1) It goes first to currentItem = 0 and slides to currentItem = 1
I don't want screen shows first 0 and slides to 1, I want screen firstly shows 1.
Is it possible with viewPager2?
答案1
得分: 1
这应该可以完成任务
viewPager.setCurrentItem(1, false) 
//smoothScroll – true to smoothly scroll to the new item, false to transition immediately
英文:
This should do the trick
viewPager.setCurrentItem(1, false) 
//smoothScroll – true to smoothly scroll to the new item, false to transition immediately
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论