在Kotlin中,是否可以使用Intent设置ViewPager2的第一个显示页面?

huangapple go评论119阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月8日 19:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定